-1

I am making an aircraft game and I need to the get the Yaw of the aircraft. Below is working code to set pitch and roll but I can't quite figure out the yaw.

private void _CalculateRollAndPitchAngles()
{
    // Calculate roll & pitch angles
    // Calculate the flat forward direction (with no y component).
    Vector3 flatForward = transform.forward;
    flatForward.y = 0;
    // If the flat forward vector is non-zero (which would only happen if the plane was pointing exactly straight upwards)
    if (flatForward.sqrMagnitude > 0)
    {
        flatForward.Normalize();
        // calculate current pitch angle
        Vector3 localFlatForward = transform.InverseTransformDirection(flatForward);
        m_pitchDegrees = Mathf.Atan2(localFlatForward.y, localFlatForward.z) * Mathf.Rad2Deg;
        // calculate current roll angle
        Vector3 flatRight = Vector3.Cross(Vector3.up, flatForward);
        Vector3 localFlatRight = transform.InverseTransformDirection(flatRight);
        m_rollDegrees = Mathf.Atan2(localFlatRight.y, localFlatRight.x) * Mathf.Rad2Deg;
    }
}
StaceyGirl
  • 6,826
  • 12
  • 33
  • 59
Gullie667
  • 103
  • 10

1 Answers1

0

Following on from comments, you can convert a transformation matrix (or its rotational part) directly into Euler angles, using formulas from here and accounting for Y-Z swapping:

roll = atan2(M[1][2], M[1][1])
pitch = atan2(-M[1][0], sqrt(M[0][1] * M[0][1] + M[0][0] * M[0][0]))
yaw = atan2(M[2][0], M[0][0])
meowgoesthedog
  • 13,415
  • 4
  • 20
  • 34
  • Although euler angles represent a rotation in space they don't work for measuring over time. If you have experience using them, you would realize this quite quickly. – Gullie667 May 29 '18 at 01:50
  • @Gullie667 not sure what you mean by that. But I re-read your question and it seems you are not trying to obtain *from* Euler angles (roll pitch yaw is already a Euler angle representation), but rather trying to compute angles from the transformation matrix. Your wording is confusing and contradicts your code. – meowgoesthedog May 29 '18 at 02:50
  • Per your suggestion, I changed "eulerAngles" to transform in the title. – Gullie667 May 29 '18 at 09:32
  • Excuse my ignorance but M? What is M[x][x]?? If it is anything like: 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); then this doesn't work when the orientation of the aircraft is pointed near vertical or straight down as the yaw is no longer accurate. – Gullie667 May 30 '18 at 02:13
  • @Guillie667 `M` is your transformation matrix; that is an intrinsic singularity of Euler angles which can only be resolved with some kind of "temporal coherence" approach, i.e. using the last valid value. Quoting you: *If you have experience using them, you would realize this quite quickly.* – meowgoesthedog May 30 '18 at 08:16
  • Not sure unity exposes this transformation matrix you speak of.... and no experience using them so its new to me. – Gullie667 May 31 '18 at 07:50
  • @Gullie667 [`Transform.localToWorldMatrix`](https://docs.unity3d.com/ScriptReference/Transform-localToWorldMatrix.html) – meowgoesthedog May 31 '18 at 10:12