简体   繁体   English

将欧拉角增加到超过360度

[英]Incrementing Euler angles beyond 360 degrees

I'm a bit confused about some documentation for Unity pertaining to Euler angles. 我对一些有关Euler角度的Unity文档感到困惑。 I just want to know if I'm not understanding a difference, or the sample does not follow the best practice. 我只是想知道我是否不了解差异,还是样本不遵循最佳实践。 The documentation here states: 这里的文档指出:

Only use this variable to read and set the angles to absolute values. 仅使用此变量读取角度并将其设置为绝对值。 Don't increment them, as it will fail when the angle exceeds 360 degrees. 不要增加它们,因为当角度超过360度时,它将失败。 Use Transform.Rotate instead. 请改用Transform.Rotate。

Meanwhile, the code sample appears to be using increments that could exceed 360 degrees: 同时,代码示例似乎正在使用可能超过360度的增量:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public float yRotation = 5.0F;
    void Update() {
        yRotation += Input.GetAxis("Horizontal");
        transform.eulerAngles = new Vector3(10, yRotation, 0);
    }
    void Example() {
        print(transform.eulerAngles.x);
        print(transform.eulerAngles.y);
        print(transform.eulerAngles.z);
    }
}

Wouldn't incrementing a variable and then using that variable to set the value absolutely still run the risk of exceeding 360 degrees if the variable is over 360 degrees? 如果变量超过360度,是否要增加变量然后再使用该变量来设置值仍然绝对会有超过360度的风险?

When you rotate your Object using euler angles then when it reaches 360 and try to exceed further, it becomes (minus)-360 and gradually increase from -359 to -1. 当您使用欧拉角旋转对象时,当对象达到360并尝试进一步超过它时,它变为(负)-360,并从-359逐渐增加到-1。

After executing following code your values will not exceed from 360 and will remain positive. 执行以下代码后,您的值将不会超过360,并且将保持正值。

float rotateAngle = rotateObject.transform.localEulerAngles.y;
// Convert negative angle to positive angle
rotateAngle = (rotateAngle > 180) ? rotateAngle - 360 : rotateAngle;
rotateObject.transform.localEulerAngles = new Vector3(rotateObject.transform.localEulerAngles.x, rotateAngle, rotateObject.transform.localEulerAngles.z);

There are differences. 有区别。 When doing: 进行时:

transform.eulerAngles.y += 1F;

You are invoking the += operator in Vector3 . 您正在调用Vector3+=运算符。

However, when you set eulerAngles this way: 但是,当您eulerAngles这种方式设置eulerAngles时:

float newY = transform.eulerAngles.y + 1F;
transform.eulerAngles = new Vector3(.., newY, ..);

You are invoking a setter in Transform , and inside this setter, it probably includes the action of updating Transform.rotation . 您正在Transform中调用设置器,并且在该设置器内部,它可能包括更新Transform.rotation的操作。

The difference is that Unity can implement the updating logic in Transform class instead of the Vector3 class, which makes much more sense there. 区别在于Unity可以在Transform类而不是Vector3类中实现更新逻辑,这在这里更有意义。

We can verify this further below in the documentation : 我们可以在下面的文档中对此进行进一步验证:

Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations. 不要单独设置eulerAngles轴之一(例如eulerAngles.x = 10;),因为这会导致漂移和不希望的旋转。 When setting them to a new value set them all at once as shown above. 将它们设置为新值时,请立即将它们全部设置,如上所示。 Unity will convert the angles to and from the rotation stored in Transform.rotation. Unity会将角度与存储在Transform.rotation中的旋转之间来回转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM