Convert Quaternion to Eulerangles

Hello, in my game I have to compare two y-rotation values of an object before and after an action.

Little bit of my code:

Function Update()

var firstRotation = pivotPoint.transform.localEulerAngles.y; // pivotPoint is the gameObject wich I need the y-rotation values off

// after the action

var secondRotation = pivotPoint.transform.localEulerAngles.y;

rotationValue = firstRotation - secondRotation;

This works great except when the secondrotation is less or equal to 360. I’ve looked it up in the script refecence and it says : Don’t increment Eulerangles, as they will fail when the angle exceeds 360 degrees.
That explains the problem.

If I use the Quaternions I get 4 strange numbers.
Is there any way I can convert these 4 numbers to the y-rotation value?

eulerAngles and localEulerAngles aren’t real variables - they actually are calculated by Unity based on transform.rotation and transform.localRotation, using the method you’re looking for. But there are several 3-axes combinations that result in the same quaternion, thus Unity chooses one of them when returning the eulerAngles: a 180 degrees rotation around Y may result in (180, 0, 180), for instance, thus the after-before angle difference may give very weird results. On the other hand, assigning angles to eulerAngles or localEulerAngles always works fine.

If you need to know the exact angles all the time, you could do the contrary: have your own eulerAngles variable, rotate them in degrees by math and assign them modulo 360 to transform.eulerAngles - something like this version of Rotate:

var myEuler: Vector3;

function Start(){
    myEuler = transform.localEulerAngles; // start with the initial rotation

function Rotate(ax: float, ay: float, az: float){
    myEuler = myEuler + Vector3(ax,ay,az); // rotate angles
    // assign the new angles in modulo 360:
    transform.localEulerAngles = Vector3(myEuler.x%360,myEuler.y%360,myEuler.z%360);

This is a local Rotate; if you use transform.eulerAngles, the rotation is world referenced.

SIDENOTE: Any 3-axes rotation can be converted to one single rotation around a specific axis, and that’s what a quaternion encodes in its mysterious four elements (x, y, z, w): the w component means cos(angle/2), and x, y and z are the axis direction multiplied by sin(angle/2). Unity has functions to convert a quaternion to its angle axis representation and vice versa, thus you don’t need to make this calculation by hand.

This is an awkward aspect of Unity. The zero crossing condition for euler angles is a pain. As it stands trying to compare one objects rotation to another objects is way more code than it needs to be. Using mod 360 is helpful, but it does not help with the zero crossing issue. I thought a lot about this and an elegant approach did not come to me. If there is some code that handles general case elegantly, I would like to see it.

I coded around my issue by using a different control interface. It turned out to be way better for me this time, but the euler angle zero crossing issue is still a pain.