- Home /

# 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?

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!

**Answer** by aldonaletto
·
Nov 06, 2011 at 11:02 PM

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.

**Answer** by sdmodiano
·
Nov 07, 2011 at 04:50 PM

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.

3-axes rotation is redundant: there are several, maybe infinity combinations that lead to the same result. That's why converting a quaternion back to 3-axes (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.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Unity Simulate Local Rotation 0 Answers

Fixing the rotation around a particular axis using Quaternions? 1 Answer

Complicated Rotation Issue 1 Answer

Setting an objects angle about a local axis without localEulerAngles? 0 Answers

Using Quaternion.LookRotation( ) on y axis only, but keep x axis of transform the same? 2 Answers