Hello, in my game I have to compare two yrotation values of an object before and after an action. Little bit of my code:
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 yrotation value?
(comments are locked)

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 3axes 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 afterbefore angle difference may give very weird results. On the other hand, assigning angles to eulerAngles or localEulerAngles always works fine. 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.
(comments are locked)

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. 3axes rotation is redundant: there are several, maybe infinity combinations that lead to the same result. That's why converting a quaternion back to 3axes (Euler angles) gives so many weird results  Unity has to use some internal criteria to choose one of them. When the rotation is done around one root axis only (X, Y or Z) you may (not without pain and tears) create some correction algorithm; if the rotation affects all 3 axes, however, forget about it: the euler angles show absolutely unpredictable values! The only way is to do the inverse thing, like suggested in my answer.
Nov 07 '11 at 10:21 PM
aldonaletto
(comments are locked)

Look up Quaternions on the Wikipedia, it gives a detailed explanation of what those 4 numbers actually mean. Needless to say, they don't really translate that well to things you'd be used to. Quaternions are to imaginary numbers what imaginary numbers are to real numbers!