I want to read the rotation of my VR headset, to lock the in-game holster if someone bends over too much (holster rotates with view direction, so if you look back upside down the holster flips a lot).
In the inspector I see the value going from 0 to 80 to 120 (see image for INSpector).
If I use transform.eulerAngles or transform.localEulerAngles I get a peak of 80 and when I bend over further the value goes back up, which does not work for me.
The code works flawlessly if I use UnityEditor.TransformUtils.GetInspectorRotation(camTransform.transform).x, but that won’t work in builds of course.
Issue visualized (sorry, I’m a dev):
Code:
if (camTransform.localEulerAngles.x <= 80) { //Lock holster }
Best visualizations ever! Every bit helps for communications.
So this is gimbal lock. You cannot read values back from eulerAngles if they go beyond 90 degrees, and even then here are other caveats. Here’s why:
Instead, keep track of your own notion of angle to make your decision upon.
If you simply do not have that notion (eg, you’re getting this rotation from some external source, like a VR controller), then you can recompute the proper tilt euler angle for perhaps enough of your cases.
In your case it may only require you to observe the sign (negative or positive… use Mathf.Sign(), not to be confused with Mathf.Sin()!!!) of the y component of one of the transform vectors (transform.forward for instance) and based on that sign, either:
use the eulerangle directly (0 to 90)
subtract the eulerangle from 180 before using it
For your use I imagine this might be all you need.