Search Unity

Resolved Why transform.localRotation.eulerAngles.x != rotation number in x axis in EDITOR

Discussion in 'Scripting' started by P4r0, May 12, 2022.

  1. P4r0

    P4r0

    Joined:
    Jun 19, 2018
    Posts:
    7
    Hi,

    I have a question, If you want to acess local position in any axis of transform, u can use transform.localPosition.axis (C#) and it gives you same number what will you see in Editor. Why when I try to acess rotation in x axis (Im using transform.localRotation.x or transform.localRotation.eulerAngles.x) it gives me different number than I see in Editor?

    (transform.localRotation.eulerAngles.x)
    Number in Editor = 126.457
    Number from code = 53.54312

    I know it is "gimbal lock problem", but why Unity can't just give me exact number what I see in Editor?

    Thanks.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,498
    This isn't physics. I'll move your post to the scripting forum.
     
    P4r0 and Bunny83 like this.
  3. Deleted User

    Deleted User

    Guest

    https://docs.unity3d.com/2017.4/Documentation/Manual/QuaternionAndEulerRotationsInUnity.html

    "Euler angles have a simpler representation, that being three angle values for X, Y and Z that are applied sequentially"

    "Quaternion representation internally consists of four numbers (referenced in Unity as x, y, z & w) however these numbers don’t represent angles or axes"

    "
    Unity stores all GameObject rotations internally as Quaternions, because the benefits outweigh the limitations.

    In the Transform Inspector however, we display the rotation using Euler angles, because this is more easily understood and edited. New values entered into the inspector for the rotation of a Game Object are converted “under the hood” into a new Quaternion rotation value for the object."

    "
    Unity converts rotational values to quaternions to store them because quaternion rotations are efficient and stable to compute. The Editor doesn’t display rotations as quaternions because a single quaternion can’t represent a rotation greater than 360 degrees about any axis.

    You can use quaternions directly if you use the Quaternion class. If you use script for your rotations, you can use the Quaternion class and functions to create and change rotational values. You can apply values to your rotation as Euler angles but you need to store them as quaternions to avoid problems."
     
    Bunny83 likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,005
    Euler angles are one way to represent an orientation (relative rotation from a reference orientation) in 3d space. However euler angles are not uniquely defined since angles are defined in a circular / wrapping space. 0° and 360° are the same angle. Likewise 180° and -180° are the same angle. When it comes to euler angles it becomes even more convoluted. Flipping all 3 euler angles by 180° gives you the same orientation.

    Unity does not work with euler angles internally but instead uses quaternions. Quaternions are not 3 seperate rotations executed one after the other like euler angles but a quaternion represents a single rotation around an arbitrarily specified axis. As you may know, you can get from any orientation to any other orientation by a single rotation around a specific axis. That's what quaternions actually do. Now when you're reading the eulerAngles property from a Quaternion, Unity has to convert the quaternion to euler angles. As mentioned in the first paragraph, there are always multiple possible combination of angles to represent the same orientation. So it's not guaranteed that you get a consistent result. It's always a correct result, but euler angles can not be viewed individually. The 3 angles together form the orientation. So the rotation 180,0,180 can also be expressed as 0,180,0. So when converting the orientation you may get any of the valid combinations which may not be continuous if you do this several time when the object rotates as depending on the numbers you may get one combination one time and another the other time.

    In your example, when you add
    126.457
    to
    53.54312
    you will notice they sum to 180°. That means the other 2 angles also have flipped.

    The Unity editor, since it's an authoring tool, also stores the local euler angles internally. However those are just for us humans when we set the angles in the inspector. When the rotation is actually changed / calculated at runtime, you can not rely on a particular combination. So there is no globally unique and valid combination of euler angles you can read. When you read the euler angles you get some euler angles representation, but it doesn't have to be consistent.

    Just thinking about spherical coordinates and limting each axis to a certain range, what limits would you pick? 0-360? -180 - +180? This would already be an issue because on one axis you only need a half turn. If you think in latitude longitude coordinates, longitude goes full 360° so from -180 to +180. However latitude only goes from -90 to +90. Going further would overlap with a different combination on the other side. So 30° longitude and100° latitude (so we've gone over the north pole) is actually -150° longitude and 80° latitude. Here we specify that latitude is restricted to an 180° range so it becomes unique. You can transform the euler angles so they follow the same pattern, but it requires quite a bit of conversion.

    So the ultimate question is: What are you want to achieve in the end? You can not look at the rotation around a single euler angles axis individually.
     
    P4r0 likes this.