0

I am making an aircraft game and I need to the get the change in Yaw of the aircraft.

I am attempting to save the previous frame's transform.eulerAngles and push them back to local space of the transform so I can capture the change. I am trying transform.InverseTransformXXXXX but it doesn't seem to work.

private float _GetChangeInYawAngle()
{
    float yaw = transform.InverseTransformDirection(m_lastForwardParented).y;
    yaw = yaw > 180 ? yaw - 360 : yaw;
    m_lastForward = transform.parent.eulerAngles;
    return yaw;
}
Gullie667
  • 133
  • 11
  • You need to clarify what you mean - yaw for an aircraft is usually the amount the nose is pointing left or right of its velocity relative to the frame of the aircraft (i.e. you use the same amount of rudder to correct a given yaw irrespective of roll or pitch), none of which is mentioned in your code. We don't know whether transform or forward are plane to world or velocity to world or plane to velocity. – Pete Kirkham May 29 '18 at 13:10

2 Answers2

0

There is no need to use InverseTransformDirection,

float _lastYaw;
void Update()
{
  float thisYaw=transform.eulerAngles.y;
  float deltaYaw=thisYaw-_lastYaw;
 _lastYaw=thisYaw;
}

Of course you still need to handle 0-360 crossing in some way.

I may be misinterpreting what you are trying to achieve here, but if you are just trying to track current rotation against last rotation, the difference between eulerAngles and localEulerAngles handles your local-world space conversion automagically

zambari
  • 4,797
  • 1
  • 12
  • 22
0

@zambari

After looking at your answer I did some research into transforming quaternions. The issue with your solution is that "thisYaw-_lastYaw" isn't pushed to local space and therefore the y axis can change wildly from frame to frame, thus the calculated difference can be much grater than the actual change. Moving the equation to local space resolves that issue unless there is a very high angular velocity, which I don't need to solve for. (In hind site, I guess I could have also used angular velocity to find this but I digress.)

Here is my working solution! Thanks for the input.

private float _SetChangeInYawAngle()
{
    Quaternion LocalRotation = Quaternion.Inverse(m_lastYawRotation) * transform.rotation;

    float yaw = LocalRotation.eulerAngles.y;

    yaw = yaw > 180 ? yaw - 360 : yaw;

    m_currentAngle -= yaw;

    m_lastYawRotation = transform.rotation;

    return m_currentAngle;
}
Gullie667
  • 133
  • 11