Unity 3D - 旋转

方法1:物体绕Y轴旋转30度。

transform.rotation *= Quaternion.AngleAxis(30, Vector3.up); 

或者

transform.localEulerAngles += new Vector3(0, 30, 0);

方法2:设置Y角度为30度。

transform.rotation = Quaternion.AngleAxis(30, Vector3.up); 

或者

float x = transform.localEulerAngles.x; 
float z = transform.localEulerAngles.z;  
transform.localEulerAngles = new Vector3(x, 30, z); 

方法3:让物体Y轴角度直接增加30度。

Quaternion qua = Quaternion.Euler(0f,30f,0f) * transform.rotation; 
transform.rotation = qua; 

方法4:让物体绕Y轴旋转30度。有旋转过程

Quaternion qua; public float RotationSpeed=0.1f;//旋转速度. 
float t;
void Start () 
{ 
    qua = Quaternion.Euler(0f,30f,0f) * transform.rotation; 
} 
void Update () 
{
    t = RotationSpeed / Quaternion.Angle (transform.rotation, qua) * Time.deltaTime; 
    transform.rotation = Quaternion.Slerp (transform.rotation, qua,t);  
 } 

方法5:让物体绕Y轴旋转面向一个点,有旋转过程

Quaternion targetRotation;
public float RotationSpeed = 50f;//旋转速度.
public Vector3 targetPoint;//要旋转面向的点.
void Start()
{ 
     targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
} 
void Update()
{ 
     this.transform.rotation = Quaternion.Lerp(this.transform.rotation, targetRotation, Time.deltaTime * RotationSpeed);
} 

总结:
四元数A * 四元数B = B * A (满足交换律):将A(B)的旋转递增B(A)的旋转。
有过程的旋转,需要计算出目标的四元数,之后运用 Quaternion.Slerp 或者 Quaternion.Lerp

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值