Unity中的旋转通常可以用Transform 直接控制和 rotation 控制两种方式。
一、Transform控制(工程中的scene1)
1.1 Transform.Rotate
旋转某个角度
函数定义
- public void Rotate(Vector3 eulerAngles);
- public void Rotate(Vector3 axis, float angle);
- public void Rotate(Vector3 eulerAngles, Space relativeTo);
- public void Rotate(float xAngle, float yAngle, float zAngle);
- public void Rotate(Vector3 axis, float angle, Space relativeTo);
- public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo);
这个函数通常用于Update函数中,用于不断的旋转。如下
- void Update () {
-
- transform.Rotate(0, rotateSpeed * Time.deltaTime, 0);
- }
1.2 Transform.RotateAround以某点为中心旋转。
函数定义
- public void RotateAround(Vector3 axis, float angle);
- public void RotateAround(Vector3 point, Vector3 axis, float angle);
这个函数通常也在Update中,不断地围绕着点运动。如下:
- void Update ()
- {
-
-
- transform.RotateAround(transformCenter.position,Vector3.up,rotateSpeed * Time.deltaTime);
- }
1.3 Transform.eulerAngles设置物体的欧拉角为某个度数
属性定义
- public Vector3 eulerAngles { get; set; }
直接设置即可,一般不用于Update函数中递增。如下
- void Update ()
- {
-
-
-
-
- transform.eulerAngles = new Vector3(0,0,60);
- }
1.4 Transform.LookAt
旋转物体,使其朝向某个位置
函数定义
- public void LookAt(Transform target);
- public void LookAt(Vector3 worldPosition);
- public void LookAt(Transform target, Vector3 worldUp);
- public void LookAt(Vector3 worldPosition, Vector3 worldUp);
简单例子:
- public class SimpleRotate4 : MonoBehaviour
- {
-
- public float rotateSpeed = 20;
- public float moveSpeed = 4;
- private float curY;
- private bool moveDown = false;
- public Transform transformCenter;
- void Update ()
- {
-
- moveDown = moveDown ? curY > -4 : curY > 4;
- curY -= moveDown ? Time.deltaTime * moveSpeed : -Time.deltaTime * moveSpeed;
- transform.position = new Vector3(-2, curY, 0);
-
-
-
- transform.LookAt(transformCenter);
- }
- }
1.5 Transform.forward强制改变z轴朝向。
属性定义
- public Vector3 forward { get; set; }
例子:
- public class SimpleRotate5 : MonoBehaviour
- {
- public float rotateSpeed = 20;
- public float moveSpeed = 4;
- private float curY;
- private bool moveDown = true;
- public Transform transformCenter;
-
- void Update ()
- {
-
-
- moveDown = moveDown ? curY > -4 : curY > 4;
- curY -= moveDown ? Time.deltaTime * moveSpeed : -Time.deltaTime * moveSpeed;
- transform.position = new Vector3(2, curY, 0);
-
-
-
- transform.forward = transformCenter.position - transform.position;
-
- }
- }
二、Rotation 与 Quaternion(对应scene2)2.1 Quaternion.eulerAngles
四元数的欧拉角
属性定义
- public Vector3 eulerAngles { get; set; }
实例
- void Update ()
- {
-
-
- Quaternion q1 = Quaternion.identity;
- q1.eulerAngles = new Vector3(0,60,0);
- transform.rotation = q1;
- }
2.2 Quaternion.SetFromToRotation (建议参考scene2场景来学习)创建一个从fromDirection到toDirection的旋转
函数定义
- public void SetFromToRotation(Vector3 fromDirection, Vector3 toDirection);
实例:
- public class QuaternionRotate2 : MonoBehaviour
- {
- public Transform A;
- public Transform B;
- public Transform C;
- public float moveSpeed = 4;
- private float curY;
- private bool moveDown = false;
- private Quaternion q1 = Quaternion.identity;
- void Update ()
- {
- moveDown = moveDown ? curY > -4 : curY > 4;
- curY -= moveDown ? Time.deltaTime * moveSpeed : -Time.deltaTime * moveSpeed;
- B.position = new Vector3(4, curY, 0);
-
-
- q1.SetFromToRotation(A.position,B.position);
- C.rotation = q1;
-
- Debug.DrawLine(Vector3.zero, A.position,Color.red);
- Debug.DrawLine(Vector3.zero, B.position, Color.green);
- Debug.DrawLine(C.position, C.position + new Vector3(0,2,0), Color.black);
- Debug.DrawLine(C.position, C.TransformPoint(0,2,0), Color.blue);
- }
- }
2.3 Quaternion.SetLookRotation旋转物体到某个角度
函数定义
- public void SetLookRotation(Vector3 view);
- public void SetLookRotation(Vector3 view, Vector3 up);
实例:
- public class QuaternionRotate3 : MonoBehaviour
- {
- private Quaternion q1 = Quaternion.identity;
- public Transform transformCenter;
- void Update ()
- {
-
- q1.SetLookRotation(transformCenter.position);
- transform.rotation = q1;
- }
- }
2.4 Quaternion.AngleAxis函数定义
- public static Quaternion AngleAxis(float angle, Vector3 axis);
围绕axis轴,旋转angle度。实例:
- void Update ()
- {
-
- transform.rotation = Quaternion.AngleAxis(60,Vector3.up);
- }
2.5 Quaternion.Slerp球形插值。
函数定义
- public static Quaternion Slerp(Quaternion a, Quaternion b, float t);
可以用于旋转值的不断靠近。如下:
- public class QuaternionRotate5 : MonoBehaviour
- {
- public Transform A;
- public float rotateSpeed = 10;
- void Update () {
- transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(A.position - transform.position), 0.8f);
- }
- }