- Home /

# finding pitch/roll/yaw from Quaternions

I'm using YEI 3-Space Sensor which gives me x,y,z,w Quaternions. Converting that to Euler angles is done by assigning Quaternions to an object then you can see Euler angles. I thought I could use Euler format as pitch/roll/yaw, but since Euler is dependent on the sequence those rotations are applied.. I now have only "Pitch"(X) since it's the first applied rotation.

I've seen matrices to solve this but isn't there just a straight forward 1 or 2 equations to solve this?

I think I just need pointing to where i should be looking.

**TL;DR: if you have Quaternions ,Euler Angles and pitch. How would you find pitch/roll/yaw?**

Thanks!

the local euler angles are pitch, roll, yaw

I thought I could use Euler format as pitch/roll/yaw ... you can

**Answer** by Raikenkun
·
Mar 13, 2013 at 11:53 PM

I guess I should avoid using Euler angles just because you may face gimbal lock issues. I solved it using these equations that use Quaternion(x,y,z,w):

```
roll = Mathf.Atan2(2*y*w - 2*x*z, 1 - 2*y*y - 2*z*z);
pitch = Mathf.Atan2(2*x*w - 2*y*z, 1 - 2*x*x - 2*z*z);
yaw = Mathf.Asin(2*x*y + 2*z*w);
```

source: http://www.tinkerforge.com/en/doc_v1/Software/Bricks/IMU_Brick_CSharp.html

although I have not understood why roll and pitch values range from 3.14 to -3.14 and yaw is 1.52~ to -1.52, but anyway it's working like a charm ! thanks for the help :)

I'm glad they work for your application. I saw them out there as a way to extract euler angles. One reference mention the angles not being what you expect with these equations but good enough for debugging, so I expected them to have similar problems to directly accessing eulerAngles.

The angles range between 3.14 and -3.14 because they're in radians. To convert them to degrees just multiply by 180/pi

Note also that in unity the yaw is not the z axis but the y axis so in this case the roll should be the Asin call and not the yaw

This doesn't look right. For roll and pitch there should be + ins$$anonymous$$d of -. It should be,

```
roll = $$anonymous$$athf.Atan2(2*y*w + 2*x*z, 1 - 2*y*y - 2*z*z);
pitch = $$anonymous$$athf.Atan2(2*x*w + 2*y*z, 1 - 2*x*x - 2*z*z);
yaw = $$anonymous$$athf.Asin(2*x*y + 2*z*w);
```

See equations in https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles

**Answer** by ratneshpatel
·
Feb 14, 2020 at 08:07 AM

```
Quaternion q = transform.rotation;
float Pitch = Mathf.Rad2Deg * Mathf.Atan2(2 * q.x * q.w - 2 * q.y * q.z, 1 - 2 * q.x * q.x - 2 * q.z * q.z);
float Yaw = Mathf.Rad2Deg * Mathf.Atan2(2 * q.y * q.w - 2 * q.x * q.z, 1 - 2 * q.y * q.y - 2 * q.z * q.z);
float Roll = Mathf.Rad2Deg * Mathf.Asin(2 * q.x * q.y + 2 * q.z * q.w);
```

Pitch - yaw - Roll :: X - Y - Z If you want the values in Radians don't multiply with Mathf.Rad2Deg

**Answer** by robertbu
·
Mar 13, 2013 at 05:08 PM

There is a long post with the issues associated with getting pitch/roll/yaw from a Quaternion here:

http://forum.unity3d.com/threads/63498-roll-pitch-and-yaw-from-quaternion

There is some code at the bottom that works for that programmer's frame of reference.

Note you do not have to assign a Quaternion to an object to get euler angles. You can use Quaternion.eulerAngles.

Thanks for the help!, yep local euler angles are doing the job, but then I run into gimbal lock problem where one of the axis just has 180 degree range ins$$anonymous$$d of the full 360 range, is there a way to solve this ? Thanks again

Did look at the code at the bottom of the link I provided, and did you read the post? The code at the bottom of the page is going about is in a ugly fashion by creating and destroying game objects, but I think he has the right idea. You need to use the axis order and unwind the Quaternion. According to the post, the axis order is Z-X-Y. You can not depend on the Quaternion.eulerAngles to output angles that make sense for direct use for pitch/roll/yaw.

I don't have time at the moment to code up an example. If you don't get it fixed (and if someone else doesn't help you), I can work up example code tomorrow.

**Answer** by kunqiyu
·
Mar 11, 2021 at 11:31 AM

since the method below are trans quaternions to eurler angle ,this may cause gimbal lock in some case

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