Skip to content

Instantly share code, notes, and snippets.

@hariedo
Created March 29, 2024 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hariedo/d15d516c9f58c6627c613da2abfc8cef to your computer and use it in GitHub Desktop.
Save hariedo/d15d516c9f58c6627c613da2abfc8cef to your computer and use it in GitHub Desktop.
public static Quaternion YawPitchRoll(float yaw, float pitch, float roll)
{
Quaternion heading = Quaternion.AngleAxis(yaw, Vector3.up);
Quaternion attitude = Quaternion.AngleAxis(pitch, Vector3.right);
Quaternion bank = Quaternion.AngleAxis(roll, Vector3.forward);
return heading * attitude * bank;
// transform.rotation *= YawPitchRoll(yaw, pitch, roll);
///// transform.rotation *= Quaternion.Euler(pitch, yaw, roll);
//
// The Euler() function also applies three independent rotations.
// There would be a natural expectation that you could use the Euler
// angles to specify such a straightforward rotation. However,
// the order of rotations hardwired into Unity's function will cause
// a gimbal lock situation and much confusion for the rotations.
// The built-in Unity ordering for Quaternion.Euler() is Z, X, Y.
// We want Y, X, Z.
//
// (Clarification: there is *always* a gimbal lock situation for
// applying Euler angles in any order, but which order you choose
// will determine what scenarios are likely to end up locked with
// two axes merged into a single pole. The Unity default ordering
// is simply not helpful for flight-simulator type applications,
// so we avoid it with this calculation instead.)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment