Unity中旋转对象的几种方式#自我总结#

目录

错误用法

使用建议

transform.eulerAngles

transform. localEulerAngles

transform.rotation

transform.localRotation

transform.Rotate( )


错误用法

 错误用法正确用法说明
获取 对象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.LookRotationQuaternion.AngleQuaternion.EulerQuaternion.SlerpQuaternion.FromToRotationQuaternion.identity

transform.localRotation

public Quaternion rotation;

  • //四元数 
  • //相对角度
transform.localRotation = Quaternion.Euler(new Vector3(x, y, z));

 

transform.Rotate( )

  • 旋转的顺序是:Z,X,Y

public void Rotate(Vector3 eulerAnglesSpace 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 zAngleSpace relativeTo = Space.Self);

transform.Rotate(angle, 0, 0); //绕自身x轴旋转angle度。         

public void Rotate(Vector3 axis, float angleSpace relativeTo = Space.Self);

  • axis 旋转 angle 角度。
transform.Rotate(Vector3.up, angle);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值