How do I get accurate SteamVR Vive controller X axis rotation euler values?

As the title says, I need to find the current x rotation of a Vive controller.

I’m currently able to get accurate Y and Z rotation values, but not X, the values will be going down, get to maybe 270 and then go up again to 360 and then go down again until it loops around to 270 at which point it’ll go up again until going down once it again get’s to 360.

The end result being that the controller rotation is 360 at two points in a full rotation. At opposite ends of the rotation too and the rest of the rotation appears accurate, meaning that when it is facing down the value will be on the lower side of 180 and when it is facing up it will be above that. But 180 is never actually seen because it is 360 at that point for some reason.

I’m not sure if it’s possible to understand exactly what I mean from the above statement, but the point is that the X rotation Euler angles are inaccurate.

To elaborate on what I need, a good analogy would be, if the controller was the headset and it was strapped to the users head, I would need values to represent how far left or right the user is looking and how far up and down the user is looking. Right now the left and right value is accurate, but what I’m assuming must be the up and down value doesn’t work properly.

The code I’m using right now is
public Vector3 Angle { get { return controller.transform.rot.eulerAngles; } }

And then a simple Debug.Log(Angle) to print that out at every update, which is how I checked what the values were in response to my rotation. Making sure that when testing any specific axis I was only turning the controller in that one axis instead of two at once which would give weird results.

Thanks in advance!

This issue only seems to affect the Euler angles, not the Quaternion itself. Here is a working solution!

Quaternion newRotation = Quaternion.identity; // Create new quaternion with no rotation
newRotation.x = transform.rotation.x; // Get only the X rotation from the controllers quaternion

// Output the x rotation. It should be a value between -1 and 1
Debug.Log ("newRotation.x: " + newRotation.x);

// Convert to a value between 0 and 360
float xEuler = (newRotation.x + 1) * 180

// Output the x rotation as a Euler angle
Debug.Log ("xEuler: " + xEuler);

For whatever reason, this doesn’t have the issue where it shows the incorrect value when rotating.

Credit to “wheatgrinder” from this reddit post

https://www.reddit.com/r/Vive/comments/6toiem/how_to_get_each_axis_rotation_from_vive/dlmf8v3/