Created
March 29, 2024 16:01
-
-
Save hariedo/d15d516c9f58c6627c613da2abfc8cef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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