相对于父变换旋转的欧拉角(以度为单位)。
欧拉角可以通过执行围绕各个轴的三次独立旋转来表示三维旋转。在 Unity 中,这些旋转按顺序围绕 Z 轴、X 轴和 Y 轴执行。
您可以通过设置此属性来设置四元数的旋转,也可以通过读取此属性来读取欧拉角值。
使用 .eulerAngles 属性设置旋转时,务必了解尽管您提供了 X、Y 和 Z 旋转值来描述旋转,但这些值不会存储在旋转中。相反,X、Y 和 Z 值将转换为四元数的内部格式。
读取 .eulerAngles 属性时,Unity 会将四元数的内部旋转表示转换为欧拉角。由于可以使用多种方法使用欧拉角表示任何给定的旋转,因此您读回的值可能与您分配的值大相径庭。如果您试图逐步增加值以产生动画,这可能会造成混淆。
为了避免此类问题,建议您在处理旋转时避免依赖读取 .eulerAngles 时的一致结果,尤其是在尝试逐步增加旋转以产生动画时。有关实现此目的的更好方法,请参见 Quaternion * 运算符。
以下示例演示了使用基于用户输入的 eulerAngles 旋转游戏对象。该示例表明,我们从不依赖读取 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.