# Euler angles conversion issue

I’m stuck with a problem using Euler angles. Basically when I try to rotate my object about the x and z axis using

``````cube.transform.rotation = Quaternion.Euler(pitch,0,-roll);
``````

It turns fine up to 80 degrees or so at which point it begins to spin 180 degrees on the local y axis of the cube and from about 100 degrees to 180 it remains flipped around.

I’m assuming its something wrong with the quaternion conversion of the Euler angles. The data used for this is coming from an Arduino which is giving out a serial stream of pitch and roll angles from -180 to +180.

My Question:
At no point do I alter the y axis rotation so why does it spin and how can I stop it?

Is the object childed to something?

Hey kyuzumaki,
for me this works fine. Try to display your pitch and roll in inspector maybe these are the problem.

With eulers there’s various ways of representing the same rotation so the values that end up in the inspector might necessarily be the ones you put into the Quaternion.Euler function - it should look the same though. You can also get gimbal lock with eulers when they approach 90 degrees (https://www.chrobotics.com/library/understanding-quaternions)

No the object is not a child of anything.

I have modified my code to use 0-360 degrees instead of +/- 180 but it still does the spin. When I view the cube in the inspector despite setting the y value to zero in the script I can see it flip to 90 and 180 degrees.

Ideally i would like unity to do nothing clever and simply apply x then y then z angles and let me worry about gimbal lock.

I’m not a quaternion expert but am going to look at how I can construct a quaternion myself and force the angle to what I need.

Ok so progress has been virtually nil. It seems to be a bug in Unity that the x axis rotation does not work properly.

This example shows the problem, attach it to a cube in the player and watch the cubes rotation angles. When rotating about the x axis unwanted y and z rotation occur, the other axis work normally.

The only other game engine I’ve used in the past is the very outdated darkbasic. I still have it installed, and on testing x rotation works without the strange flipping.

``````usingUnityEngine;
usingSystem.Collections;

publicclassrotateBug : MonoBehaviour {
Vector3ApRotateAngles;
floatpitch=0f;
floatroll=0f;
//Usethisforinitialization
voidStart () {

}

//Updateiscalledonceperframe
voidUpdate () {

ApRotateAngles.x=pitch;
ApRotateAngles.y=0;
ApRotateAngles.z=0;
transform.eulerAngles = ApRotateAngles;

if (pitch >= 360f) {
pitch = 0f;
} else {
pitch+=1f;
}

}
}
``````

I tried to rotate about each axis one by one but alas the problem still occurs. I can not seem to prevent the unwanted 180 degree y axis rotation.

I have seen people talk about creating a cloud of quaternions for each position and somehow select from them but I cant find an example of how to do it and it sounds like overkill for the task at hand.