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

    Maygar

    Joined:
    May 22, 2018
    Posts:
    2
    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;
    2.  
    3.         [Button]
    4.         public void Test()
    5.         {
    6.             euler = transform.eulerAngles;
    7.             Debug.Log($"Euler : {transform.eulerAngles}");
    8.         }
    upload_2023-11-4_11-46-25.png

    The vector used in the shader :
    upload_2023-11-4_11-47-5.png

    Result with Euler angles of (90, 270, 0) in the shader :
    upload_2023-11-4_11-49-15.png

    Expected result with Euler angles of (90, 0, 90) in the shader :
    upload_2023-11-4_11-49-48.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,239
    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:

    https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html
     
    Maygar likes this.
  3. Maygar

    Maygar

    Joined:
    May 22, 2018
    Posts:
    2
    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 :
    upload_2023-11-4_13-54-35.png

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