我正在尝试让我的对象在特定的时间内旋转到特定的y值(0,90,180和270)。使用EulerAngles可以实现,但是当我想要从270旋转到0时,它会旋转全程。所以我决定使用四元数和刚体上的旋转。以下是函数:
IEnumerator DoRotatePlayer(float endRotation)
{
float startRotation = transform.eulerAngles.y;
float t = 0.0f;
while (t < rotationDuration) //rotationDuration = 0.5f
{
t += Time.deltaTime;
float yRotation = Mathf.Lerp(startRotation, endRotation, t / rotationDuration) % 360.0f;
var targetRotation = new Quaternion(0, yRotation, 0, 0);
//targetRotation.Normalize(); // 移除这一行
playerManager.Rb.rotation = targetRotation;
yield return null;
}
var targetRotation2 = new Quaternion(0, endRotation, 0, 0);
//targetRotation2.Normalize(); // 移除这一行
playerManager.Rb.rotation = targetRotation2;
}
问题是每次我都要对四元数进行归一化(因为“四元数旋转必须是单位长度的”),这当然会将任何非0的endRotation/yRotation值都变成1。所以在第一次旋转时,90度的旋转变成了180度。只需找到一种方法来应用旋转而不进行归一化。尝试在while循环之前将endRotation除以360没有任何效果。简单地将旋转应用到transform.rotation上,使用EulerAngles或四元数也没有效果,旋转完全不起作用。
评论 (0)