Vector3 中的 eulerAngles 导致奇怪的旋转

我正在尝试更改相机旋转的 eulerAngle X,同时将 y 和 z 保持为 0。

但是,以下 IEnumerator 会产生奇怪的 eulerAngles,例如 10、-74.653、0。

http://img3.mukewang.com/62a55b73000180e306440141.jpg

我不明白 y 值如何在以下函数中发生变化:


private IEnumerator LerpCameraNormalRot()

{

    float duration = 0.5f;


    for (float t = 0f; t < duration; t += Time.deltaTime)

    {

        float f = Mathf.Lerp(camera.transform.eulerAngles.x, 10, t / duration);

        Vector3 vNew = new Vector3(f, 0, 0);

        camera.transform.eulerAngles = vNew;

        yield return 0;

    }

}

这不是很奇怪吗?我从不改变 Y 和 Z 值!


我只想将 X 旋转 (eulerAngle.x) 从其当前值更改为 10。


感谢您的帮助!


潇潇雨雨
浏览 207回答 2
2回答

暮色呼如

您正在设置,eulerAngles但这将导致localEulerAngles图像中的红色框表示的不同。如果对象有任何旋转的祖先,它们可能不匹配!要解决您的直接问题,您可以使用localEulerAngles代替eulerAngles:float f = Mathf.Lerp(camera.transform.localEulerAngles.x, 10, t / duration);Vector3 vNew = new Vector3(f, 0, 0);camera.transform.localEulerAngles= vNew;这样做的问题是它没有考虑从 359 度到 0 度的能力。更好的是使用Quaternion.Euler和Quaternion.Slerp使用四元数而不是欧拉角:private IEnumerator LerpCameraNormalRot(){&nbsp; &nbsp; float duration = 0.5f;&nbsp; &nbsp; Quaternion startRotation = camera.transform.localRotation;&nbsp; &nbsp; Quaternion goalRotation = Quaternion.Euler(10f, 0f, 0f);&nbsp; &nbsp; for (float t = 0f; t < duration; t += Time.deltaTime)&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; camera.transform.localRotation = Quaternion.Slerp(startRotation, goalRotation, t/duration);&nbsp; &nbsp; &nbsp; &nbsp; yield return 0;&nbsp; &nbsp; }&nbsp; &nbsp; camera.transform.localRotation = goalRotation;}

MYYA

unity使用旋转、位置、eulerAngles等两种表示一个在世界空间另一个在本地空间不包括local在其名称中的值在世界空间中。如果您的对象比任何被旋转/缩放/平移的父对象具有,您将看不到您在 Unity 中设置的值,因为Transform检查器显示本地坐标。如果要将local坐标设置为精确值,请改用localPosition,localRotation或localEulerAngles。对我来说,您似乎想x在 0.5 秒内将相机围绕其局部轴旋转 10°。所以我认为你可以改为这样做private IEnumerator LerpCameraNormalRot(){&nbsp; &nbsp; float duration = 0.5f;&nbsp; &nbsp; float initialRotationX = camera.transform.localEulerAngles.x;&nbsp; &nbsp; float targetRotationX = 10;&nbsp; &nbsp; for (float t = 0f; t < duration; t += Time.deltaTime)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; float currentX = Mathf.Lerp(initialRotationX, targetRotationX, t / duration);&nbsp; &nbsp; &nbsp; &nbsp; camera.transform.localEulerAngles = new Vector3(currentX , 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; // Which would be the same as using&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // camera.transform.localRotation = Quaternion.Euler(new Vector3(currentX, 0, 0));&nbsp; &nbsp; &nbsp; &nbsp; yield return null;&nbsp; &nbsp; }&nbsp; &nbsp; // to be sure you have no overshooting you could set the target rotation fix here as well&nbsp; &nbsp; camera.transform.localEulerAngles = new Vector3(targetRotation, 0 ,0);&nbsp; &nbsp; // or&nbsp; &nbsp; // camera.transform.localRotation = Quaternion.Euler(new Vector3(targetRotation, 0, 0));}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP