[SOLVED] Get Euler angles as displayed in inspector

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 :

[SerializeField] private Vector3 euler;

        [Button]
        public void Test()
        {
            euler = transform.eulerAngles;
            Debug.Log($"Euler : {transform.eulerAngles}");
        }

9449864--1326521--upload_2023-11-4_11-46-25.png

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

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

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

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

1 Like

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 :

transform.rotation.ToAngleAxis(out float angle, out Vector3 axis);
material.SetVector("_RotationAxis", axis);
material.SetFloat("_Angle", angle);

And I changed my Shader Graph to simply apply a single rotation of “angle” around “axis” like so :
9449978--1326563--upload_2023-11-4_13-54-35.png

This created exactly the result I wanted, thanks a lot!!

1 Like