This is an age old problem and I know there are tons of posts on using quaternions for rotation, and I understand gimbal lock, however I am still missing something…
What I want to do is read the current Euler angles for a transform, however the values I am getting do not match what I see in the inspector. Whatever Unity is doing in the Transform inspector to display the (Euler) rotation values, I want to match that. I understand that under the hood rotations are Quaternions, but how is this value then converted to the Euler display? Reading the eulerAngles value isn’t cutting it.
For example, when I manually input a rotation in the inspector of [180,0,0], I expect that when I read transform.localEulerAngles (or transform.localRotation.eulerAngles) that I will also get [180,0,0], however instead what I am getting is [0,180,180]. Furthermore, the rotations wrap around from 0-360, so if the inspector shows a value such as [0,720,0], the value I get from transform.localEulerAngles is [0,0,0]. Unity is obviously calculating these values in a different way.
So the big question is: how do I get the exact same Euler rotation values shown in the inspector?
Here is a very simple script that demonstrates the problem, and also a screenshot attached showing the object and console output.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode()]
public class RotationTest : MonoBehaviour {
void Update () {
Debug.Log("Q:"+transform.localRotation+" E1:"+transform.localRotation.eulerAngles+" E2:"+transform.localEulerAngles);
}
}
I’m hoping this is a simple fix. Appreciate you taking the time to read and for any help.
Thanks,
Stephen
