Skip to content

Instantly share code, notes, and snippets.

@inoook
Last active December 22, 2015 11:13
Show Gist options
  • Save inoook/e616008feafeb1152cbb to your computer and use it in GitHub Desktop.
Save inoook/e616008feafeb1152cbb to your computer and use it in GitHub Desktop.
RollPitchYawFromQuaternion.cs
// http://answers.unity3d.com/questions/416169/finding-pitchrollyaw-from-quaternions.html
Quaternion q = shipTrans.localRotation;
float x = q.x;
float y = q.y;
float z = q.z;
float w = q.w;
float _pitch = Mathf.Atan2(2*x*w - 2*y*z, 1 - 2*x*x - 2*z*z) * Mathf.Rad2Deg;
float _yaw = Mathf.Atan2(2*y*w - 2*x*z, 1 - 2*y*y - 2*z*z) * Mathf.Rad2Deg;
float _roll = Mathf.Asin(2*x*y + 2*z*w) * Mathf.Rad2Deg;
//
float getPitch(Quaternion q)
{
float x = q.x;
float y = q.y;
float z = q.z;
float w = q.w;
return Mathf.Atan2(2*(y*z + w*x), w*w - x*x - y*y + z*z) * Mathf.Rad2Deg;
}
float getYaw(Quaternion q)
{
float x = q.x;
float y = q.y;
float z = q.z;
float w = q.w;
return Mathf.Asin(-2*(x*z - w*y)) * Mathf.Rad2Deg;
}
float getRoll(Quaternion q)
{
float x = q.x;
float y = q.y;
float z = q.z;
float w = q.w;
return Mathf.Atan2(2*(x*y + w*z), w*w + x*x - y*y - z*z) * Mathf.Rad2Deg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment