Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question [SOLVED] Get Euler angles as displayed in inspector

Discussion in 'Editor & General Support' started by Maygar, Nov 4, 2023.

  1. Maygar


    May 22, 2018
    Hi, I'm not sure in which category to put this question so if it's not in the right place, tell me and I'll move it.

    In my game I need to access the Euler angles of an object as they are displayed in the editor.
    Accessing transform.eulerAngles works most of the times, but I have an object that has (90, 0, 90) as Euler angles displayed in the inspector. When I use transform.eulerAngles on this object, I get (90, 270, 0). That is a problem since I'm using that value in a shader, and there is a big difference between the two vectors.

    How can I turn the value I get from transform.eulerAngles into the value I see in the inspector ? Without using UnityEditor functions since I need it to work in the build.

    My testing script :
    Code (CSharp):
    1. [SerializeField] private Vector3 euler;
    3.         [Button]
    4.         public void Test()
    5.         {
    6.             euler = transform.eulerAngles;
    7.             Debug.Log($"Euler : {transform.eulerAngles}");
    8.         }

    The vector used in the shader :

    Result with Euler angles of (90, 270, 0) in the shader :

    Expected result with Euler angles of (90, 0, 90) in the shader :
  2. Kurt-Dekker


    Mar 16, 2013
    That "pretty human numbers" functionality is in the editor inspector for Transforms.

    Generally don't use eulerAngles. Here's why:

    All about Euler angles and rotations, by StarManta:
    Maygar likes this.
  3. Maygar


    May 22, 2018
    Thanks for this link, it's really insightful !
    Indeed Euler angles aren't reliable, but there is no quaternion rotation in Shader Graph, hence why I wanted to use Euler angles.
    In your link, they suggest using Quaternion.AngleAxis instead, and that's what I did, and it works perfect!!

    Instead of using material.SetVector("_EulerAngles", transform.eulerAngles), I instead do this :
    Code (CSharp):
    1. transform.rotation.ToAngleAxis(out float angle, out Vector3 axis);
    2. material.SetVector("_RotationAxis", axis);
    3. material.SetFloat("_Angle", angle);
    And I changed my Shader Graph to simply apply a single rotation of "angle" around "axis" like so :

    This created exactly the result I wanted, thanks a lot!!
    Kurt-Dekker likes this.