Unity常用旋转API

Unity中的旋转通常可以用Transform 直接控制和 rotation 控制两种方式。

一、Transform控制(工程中的scene1)

1.1 Transform.Rotate

旋转某个角度

函数定义

[csharp] view plain copy

  1. public void Rotate(Vector3 eulerAngles);  
  2. public void Rotate(Vector3 axis, float angle);  
  3. public void Rotate(Vector3 eulerAngles, Space relativeTo);  
  4. public void Rotate(float xAngle, float yAngle, float zAngle);  
  5. public void Rotate(Vector3 axis, float angle, Space relativeTo);  
  6. public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo);  

这个函数通常用于Update函数中,用于不断的旋转。如下

[csharp] view plain copy

  1. void Update () {  
  2.      //以每秒rotateSpeed的速度绕着y轴旋转  
  3.      transform.Rotate(0, rotateSpeed * Time.deltaTime, 0);  
  4. }  

1.2 Transform.RotateAround

以某点为中心旋转。

函数定义

[csharp] view plain copy

  1. public void RotateAround(Vector3 axis, float angle);  
  2. public void RotateAround(Vector3 point, Vector3 axis, float angle);  

这个函数通常也在Update中,不断地围绕着点运动。如下:

[csharp] view plain copy

  1. void Update ()   
  2. {  
  3.     //在Y轴以每秒rotateSpeed的速度围绕着transformCenter 旋转。  
  4.     //这会改变物体的位置和旋转。  
  5.     transform.RotateAround(transformCenter.position,Vector3.up,rotateSpeed * Time.deltaTime);  
  6. }  

1.3 Transform.eulerAngles

设置物体的欧拉角为某个度数

属性定义

[csharp] view plain copy

  1. public Vector3 eulerAngles { getset; }  

直接设置即可,一般不用于Update函数中递增。如下

[csharp] view plain copy

  1. void Update ()  
  2. {  
  3.     //设置旋转角度为一个固定值  
  4.     //旋转作为欧拉角度。  
  5.     //x、y、z角代表绕z轴旋转z度,绕x轴旋转x度,绕y轴旋转y度(这个顺序)。  
  6.     //仅使用者这个变量读取和设置角度到绝对值。不要递增它们,当超过角度360度,它将失败。使用Transform.Rotate替代。  
  7.     transform.eulerAngles = new Vector3(0,0,60);  
  8. }  

 

1.4 Transform.LookAt

旋转物体,使其朝向某个位置

函数定义

[csharp] view plain copy

  1. public void LookAt(Transform target);  
  2. public void LookAt(Vector3 worldPosition);  
  3. public void LookAt(Transform target, Vector3 worldUp);  
  4. public void LookAt(Vector3 worldPosition, Vector3 worldUp);  

简单例子:

[csharp] view plain copy

  1. public class SimpleRotate4 : MonoBehaviour  
  2. {  
  3.   
  4.     public float rotateSpeed = 20;  
  5.     public float moveSpeed = 4;  
  6.     private float curY;  
  7.     private bool moveDown = false;  
  8.     public Transform transformCenter;  
  9.     void Update ()  
  10.    {  
  11.        //这段代码只是让物体不断的上下运动  
  12.        moveDown = moveDown ? curY > -4 : curY > 4;  
  13.         curY -= moveDown ? Time.deltaTime * moveSpeed : -Time.deltaTime * moveSpeed;  
  14.         transform.position = new Vector3(-2, curY, 0);  
  15.   
  16.         //朝向目标  
  17.         //当该物体设置了LookAt并指定了目标物体时,该物体的z轴将始终指向目标物体  
  18.         transform.LookAt(transformCenter);  
  19.     }  
  20. }  


1.5 Transform.forward

强制改变z轴朝向。

属性定义

[csharp] view plain copy

  1. public Vector3 forward { getset; }  

例子:

[csharp] view plain copy

  1. public class SimpleRotate5 : MonoBehaviour  
  2. {  
  3.     public float rotateSpeed = 20;  
  4.     public float moveSpeed = 4;  
  5.     private float curY;  
  6.     private bool moveDown = true;  
  7.     public Transform transformCenter;  
  8.       
  9.     void Update ()  
  10.     {  
  11.   
  12.         //这段代码只是让物体不断的上下运动  
  13.         moveDown = moveDown ? curY > -4 : curY > 4;  
  14.         curY -= moveDown ? Time.deltaTime * moveSpeed : -Time.deltaTime * moveSpeed;  
  15.         transform.position = new Vector3(2, curY, 0);  
  16.   
  17.         //强制设置z轴朝向  
  18.         //本例子和LookAt区别不大  
  19.         transform.forward = transformCenter.position - transform.position;  
  20.           
  21.     }  
  22. }  

 

二、Rotation 与 Quaternion(对应scene2)

2.1 Quaternion.eulerAngles

四元数的欧拉角

属性定义

[csharp] view plain copy

  1. public Vector3 eulerAngles { getset; }  

实例

[csharp] view plain copy

  1. void Update ()   
  2. {  
  3.     //功能类似于 transform.eulerAngles = new Vector3(0,0,60);  
  4.     //但是不能直接设置rotation.eulerAngles 需要一个完整的Quaternion  
  5.     Quaternion q1 = Quaternion.identity;  
  6.     q1.eulerAngles = new Vector3(0,60,0);  
  7.     transform.rotation = q1;  
  8. }  

2.2 Quaternion.SetFromToRotation (建议参考scene2场景来学习)

创建一个从fromDirection到toDirection的旋转

函数定义

[csharp] view plain copy

  1. public void SetFromToRotation(Vector3 fromDirection, Vector3 toDirection);  

实例:

[csharp] view plain copy

  1. public class QuaternionRotate2 : MonoBehaviour  
  2. {  
  3.     public Transform A;  
  4.     public Transform B;  
  5.     public Transform C;  
  6.     public float moveSpeed = 4;  
  7.     private float curY;  
  8.     private bool moveDown = false;  
  9.     private Quaternion q1 = Quaternion.identity;  
  10.     void Update ()   
  11.     {  
  12.         moveDown = moveDown ? curY > -4 : curY > 4;  
  13.         curY -= moveDown ? Time.deltaTime * moveSpeed : -Time.deltaTime * moveSpeed;  
  14.         B.position = new Vector3(4, curY, 0);  
  15.   
  16.         //创建一个从fromDirection到toDirection的旋转。  
  17.         q1.SetFromToRotation(A.position,B.position);  
  18.         C.rotation = q1;  
  19.   
  20.         Debug.DrawLine(Vector3.zero, A.position,Color.red);  
  21.         Debug.DrawLine(Vector3.zero, B.position, Color.green);  
  22.         Debug.DrawLine(C.position, C.position + new Vector3(0,2,0), Color.black);  
  23.         Debug.DrawLine(C.position, C.TransformPoint(0,2,0), Color.blue);  
  24.     }  
  25. }  

2.3 Quaternion.SetLookRotation

旋转物体到某个角度

函数定义

[csharp] view plain copy

  1. public void SetLookRotation(Vector3 view);  
  2. public void SetLookRotation(Vector3 view, Vector3 up);  

实例:

[csharp] view plain copy

  1. public class QuaternionRotate3 : MonoBehaviour   
  2. {  
  3.     private Quaternion q1 = Quaternion.identity;  
  4.     public Transform transformCenter;  
  5.     void Update ()   
  6.     {  
  7.         //相当于transform.LookAt  
  8.         q1.SetLookRotation(transformCenter.position);  
  9.         transform.rotation = q1;  
  10.     }  
  11. }  


2.4 Quaternion.AngleAxis

函数定义

[csharp] view plain copy

  1. public static Quaternion AngleAxis(float angle, Vector3 axis);  

围绕axis轴,旋转angle度。

实例:

[csharp] view plain copy

  1. void Update ()   
  2. {  
  3.     //绕y轴旋转60度  
  4.     transform.rotation = Quaternion.AngleAxis(60,Vector3.up);  
  5. }  

2.5 Quaternion.Slerp

球形插值。

函数定义

[csharp] view plain copy

  1. public static Quaternion Slerp(Quaternion a, Quaternion b, float t);  

可以用于旋转值的不断靠近。如下:

[csharp] view plain copy

  1. public class QuaternionRotate5 : MonoBehaviour  
  2. {  
  3.     public Transform A;  
  4.     public float rotateSpeed = 10;  
  5.     void Update () {  
  6.         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(A.position - transform.position), 0.8f);  
  7.     }  
  8. }  

更多unity2018的功能介绍请到paws3d爪爪学院查找。链接https://www.paws3d.com/learn/,也可以加入unity学习讨论群935714213

近期更有资深开发人士直播分享unity开发经验,详情请进入官网或加入QQ群了解

猜你喜欢

转载自blog.csdn.net/qq_35037137/article/details/88691518