【转】Unity3D Transform中有关旋转的属性和方法测试

Transform有关旋转个属性和方法测试

 

一,属性

1,var eulerAngles : Vector3

 

[csharp] view plaincopy
 
 
  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 plaincopy
 
 
  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.eulerAngles赋值。可以使用各种Quaternion的方法。

 

 

二,方法

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

 

[csharp] view plaincopy
 
 
  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 plaincopy
 
 
  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 plaincopy
 
 
  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 plaincopy
 
 
  1. public Transform target;  
  2.    void Update() {  
  3.        transform.LookAt(target);  
  4.    }  

效果:使物体绕y轴旋转,z轴一直指向target,所以顾名思义叫lookat。

posted on 2017-06-04 10:27  mimime  阅读(8208)  评论(0编辑  收藏  举报