示例一:通过四元数插值,使物体缓慢旋转。
using System.Collections;
using UnityEngine;
public class Test2 : MonoBehaviour {
public float speed = 0.2f;//旋转速度
[SerializeField]
private Rigidbody m_Rigidbody;
private Quaternion targetAngle;
private float angleLerp;
void Awake()
{
if (null == m_Rigidbody)
m_Rigidbody = this.GetComponent<Rigidbody>();
}
void Start () {
targetAngle = Quaternion.AngleAxis(60f, Vector3.up);
}
void Update()
{
angleLerp += speed * Time.deltaTime;
Quaternion q = Quaternion.Lerp(Quaternion.identity, targetAngle, angleLerp);
m_Rigidbody.MoveRotation(q);
}
}
运行测试
//创建一个旋转到forward方位的四元素 Vector3 forward = transform.forward; Quaternion quaternion = Quaternion.identity; quaternion.SetLookRotation(forward);