以欧拉角表示的相对于父变换旋转的旋转(以度为单位)。
欧拉角可以通过围绕各个轴执行三个单独的旋转来表示三维旋转。在 Unity 中,围绕 Z 轴、X 轴和 Y 轴(按该顺序)执行这些旋转。
可以通过设置此属性来设置四元数的旋转,并且可以通过读取此属性来读取欧拉角的值。
使用 .eulerAngles 属性设置旋转时,务必要了解,虽然提供 X、Y 和 Z 旋转值描述旋转,但是这些值不存储在旋转中。而是将 X、Y 和 Z 值转换为四元数的内部格式。
读取 .eulerAngles 属性时,Unity 将四元数的内部旋转表示形式转换为欧拉角。因为可通过多种方式使用欧拉角表示任何给定旋转,所以读出的值可能与分配的值截然不同。如果尝试逐渐增加值以生成动画,则这种情况可能会导致混淆。
若要避免这些类型的问题,使用旋转的建议方式是避免在读取 .eulerAngles 时依赖一致的结果,特别是在尝试逐渐增加旋转以生成动画时。有关实现此目标的更佳方式,请参阅四元数 * 运算符。
以下示例说明基于用户的输入使用 eulerAngles 旋转 GameObject。该示例说明我们从不依赖于读取 Quanternion.eulerAngles 来增加旋转,而是使用 Vector3 currentEulerAngles 来设置它。所有旋转更改都在 currentEulerAngles 变量中进行,这些更改随后应用于四元数,从而避免上面提到的问题。
using UnityEngine; public class ExampleScript : MonoBehaviour { float rotationSpeed = 45; Vector3 currentEulerAngles; float x; float y; float z;
void Update() { if (Input.GetKeyDown(KeyCode.X)) x = 1 - x; if (Input.GetKeyDown(KeyCode.Y)) y = 1 - y; if (Input.GetKeyDown(KeyCode.Z)) z = 1 - z;
//modifying the Vector3, based on input multiplied by speed and time currentEulerAngles += new Vector3(x, y, z) * Time.deltaTime * rotationSpeed;
//apply the change to the gameObject transform.localEulerAngles = currentEulerAngles; }
void OnGUI() { GUIStyle style = new GUIStyle(); style.fontSize = 24; GUI.Label(new Rect(10, 0, 0, 0), "Rotating on X:" + x + " Y:" + y + " Z:" + z, style);
GUI.Label(new Rect(10, 50, 0, 0), "Transform.localEulerAngle: " + transform.localEulerAngles, style); } }
Unity 自动在角度与存储在 Transform.localRotation 中的旋转之间进行转换。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.