目录
错误用法
错误用法 | 正确用法 | 说明 | |
获取 对象Z轴的旋转角度 | float angle_Z = transform.rotation.z; | float angle_Z = transform.eulerAngles.z; | transform.rotation是四元数,其x, y, z ,w 分量不要直接修改/使用,除非您非常了解四元数。 |
设置 对象Z轴的旋转角度为90度 | transform.rotation.z = 90; transform.eulerAngles.z =90; | transform.eulerAngles = new Vector3 ( x, y, 90); 或 transform.rotation = Quaternion.Euler(new Vector3(x, y, 90)); | 不要单独设置eulerAngles的x,y,z其中任何一个变量 |
使用建议
官方API说:
To rotate an object, use Transform.Rotate.
Use Transform.eulerAngles for setting the rotation as euler angles.
Transform.rotation will provide or accept the rotation using a Quaternion.
————————————————————————————————————
要旋转物体,使用 Transform.Rotate() 方法。
用欧拉角设置旋转角度,使用 Transform.eulerAngles 。 //可以理解为Vector3
Transform.rotation 将使用四元数提供或接受旋转。
transform.eulerAngles
public Vector3 eulerAngles;
transform.eulerAngles = new Vector3(x,y,z); //为游戏物体最终旋转到的目标角度
- 可视为 = transform面板上Rotation的x y z值(当对象没有父物体时)
- //欧拉角旋转,以度为单位。
- //绝对角度
注意:不要单独设置其中任何一个变量。仅使用此变量读取角度并将其设置为绝对值。不要增加它们,因为当角度超过360度时,它将失败。请改用Transform.Rotate。
//x,y和z角度分别表示围绕z轴旋转z度,围绕x轴旋转x度和围绕y轴旋转y度。
transform. localEulerAngles
public Vector3 eulerAngles;
transform. localEulerAngles = new Vector3(x,y,z); //面板角度(相对角度)
- // transform面板上Rotation的x y z值
- //欧拉角旋转,以度为单位。
- //相对角度
transform.rotation
public Quaternion rotation;
transform.rotation = Quaternion.Euler(new Vector3(x, y, z));
- //四元数
- //绝对角度
Quaternion rotation= Quaternion.Euler(new Vector3(x, y, z));
transform.rotation=rotation;
关于四元数:
您几乎永远不会访问或修改单个四元数组件(x,y,z,w);大多数情况下,您只是采用现有的旋转(例如从Transform),并使用它们来构建新的旋转(例如,在两个旋转之间平滑地插值)。您使用99%的时间的Quaternion函数是: Quaternion.LookRotation,Quaternion.Angle,Quaternion.Euler,Quaternion.Slerp,Quaternion.FromToRotation和Quaternion.identity。
transform.localRotation
public Quaternion rotation;
- //四元数
- //相对角度
transform.localRotation = Quaternion.Euler(new Vector3(x, y, z));
transform.Rotate( )
- 旋转的顺序是:Z,X,Y
public void Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self);
transform.Rotate(Vector3.right * s * Time.deltaTime); //绕自身x轴旋转,每秒s度
transform.Rotate(Vector3.up * s * Time.deltaTime, Space.World); //绕世界Y轴旋转,每秒s度
public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);
transform.Rotate(angle, 0, 0); //绕自身x轴旋转angle度。
public void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self);
- 绕 axis 旋转 angle 角度。
transform.Rotate(Vector3.up, angle);