zoukankan      html  css  js  c++  java
  • quaternion*Vector3的新理解

    using UnityEngine;
    using System.Collections;
    
    public class PlayerScript : MonoBehaviour {
        
        public GameObject cameraObject;
        public float cameraDistance;//the distance the camera should be palced from the palyer
        public float cameraHeight;//how heigh the camera should be
        private float cameraAngleToPlayer;// the current angle the camera is to the palyer
        private Vector3 tempVector; // the temporary vector we shall use for calcuations
        // Use this for initialization
        void Start () {
        
        }
        
        // Update is called once per frame
        void Update () {
            tempVector = Vector3.left;
            if(Input.GetKey ("left")) //rotation the angle based on input
            {
                cameraAngleToPlayer = cameraAngleToPlayer - (Time.deltaTime * 50f);
            }else if(Input.GetKey("right")){
                cameraAngleToPlayer = cameraAngleToPlayer +(Time.deltaTime *50f);
            }
            Debug.Log("Quaternion:"+Quaternion.AngleAxis(90f,Vector3.up)+";tempVector:"+tempVector);
            
            tempVector = Quaternion.AngleAxis(cameraAngleToPlayer,Vector3.up)*tempVector;//We are now rotating the Vector around the vertical(y) axis by an angle specificed in the variable 'cameraAngleToPlayer'
            Debug.Log("tempVector after calculate:"+tempVector);
            Debug.DrawLine(transform.position,tempVector*10f,Color.yellow);
            cameraObject.transform.position = transform.position + (tempVector.normalized * cameraDistance) + new Vector3(0,cameraHeight,0);
            cameraObject.transform.rotation = Quaternion.LookRotation(transform.position - cameraObject.transform.position);
        }    
    }
    以上是绕cube旋转摄像机的示例代码,这段代码拖到cube上。
    Quaternion*Vector3的几何意义

    例如

    Quaternion.AngleAxis(90f,Vector3.up)*Vector3.left

    Quaternion:(0.0, 0.7, 0.0, 0.7);Vector3.left:(-1.0, 0.0, 0.0)

    calculated:(0.0, 0.0, 1.0)

    这样我们得到了从(-1.0, 0.0, 0.0)绕y轴旋转90°的新坐标。

  • 相关阅读:
    poj 3261 Milk Patterns
    poj 3292 Semi-prime H-numbers
    bzoj千题计划256:bzoj2194: 快速傅立叶之二
    bzoj千题计划255:bzoj3572: [Hnoi2014]世界树
    bzoj千题计划254:bzoj2286: [Sdoi2011]消耗战
    bzoj千题计划253:bzoj2154: Crash的数字表格
    扩展BSGS算法
    bzoj千题计划252:bzoj1095: [ZJOI2007]Hide 捉迷藏
    bzoj千题计划251:bzoj3672: [Noi2014]购票
    bzoj千题计划250:bzoj3670: [Noi2014]动物园
  • 原文地址:https://www.cnblogs.com/88999660/p/3262656.html
Copyright © 2011-2022 走看看