Unity3D Transform中有關旋轉的屬性和方法測試

Transform有關旋轉個屬性和方法測試


一,屬性

1,var eulerAngles : Vector3

[csharp] view plain copy
  1. public float yRotation = 5.0F;  
  2.    void Update() {  
  3.        yRotation += Input.GetAxis("Horizontal");  
  4.        transform.eulerAngles = new Vector3(10, yRotation, 0);  
  5.    }  
效果:與Quaternion.enlerAngles基本相同,用來設定物體的旋轉角度,但不要分別設置xyz,要整體賦值。


2,var rotation : Quaternion

[csharp] view plain copy
  1. public float smooth = 2.0F;  
  2.    public float tiltAngle = 30.0F;  
  3.    void Update() {  
  4.        float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;  
  5.        float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;  
  6.        Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);  
  7.        transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);  
  8.    }  
效果:代表了物體的旋轉,不能直接爲transform.rotation賦值。可以使用各種Quaternion的方法。


二,方法

1,function Rotate (eulerAngles : Vector3, relativeTo : Space = Space.Self) :void 

[csharp] view plain copy
  1. void Update() {  
  2.         transform.Rotate(Vector3.right * Time.deltaTime);  
  3.         transform.Rotate(Vector3.up * Time.deltaTime, Space.World);  
  4.     }  

效果,使物體旋轉一個基於歐拉角的旋轉角度,eulerAngles.z度圍繞z軸,eulerAngles.x度圍繞x軸,eulerAngles.y度圍繞y軸。vector3可以變成3個分開的float值。


2,function Rotate (axis : Vector3, angle : float, relativeTo : Space = Space.Self) : void 

[csharp] view plain copy
  1. void Update() {  
  2.         transform.Rotate(Vector3.right, Time.deltaTime);  
  3.         transform.Rotate(Vector3.up, Time.deltaTime, Space.World);  
  4.     }  

效果:按照angle度圍繞axis軸旋轉。


3,function RotateAround (point : Vector3, axis : Vector3, angle : float) : void 

[csharp] view plain copy
  1.     public Transform target;   
  2.     Quaternion rotation;  
  3.     void Update()  
  4.     {  
  5.         transform.RotateAround(target.position, Vector3.up, 20 * Time.deltaTime);  
  6.     }  
效果: 以point爲中心點,以axis爲軸進行旋轉,類似於圍繞某一點公轉。


4,function LookAt (target : Transform, worldUp :Vector3 =Vector3.up) : void 

[csharp] view plain copy
  1. public Transform target;  
  2.    void Update() {  
  3.        transform.LookAt(target);  
  4.    }  
效果:使物體繞y軸旋轉,z軸一直指向target,所以顧名思義叫lookat。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章