Search Unity

Getting climb angle of my airplane

Discussion in 'Scripting' started by chris64, Sep 4, 2012.

  1. chris64

    chris64

    Joined:
    Sep 27, 2011
    Posts:
    72
    Is there a simple way to get the climb/yaw angle of my plane? I can't seem to figure this out and it's driving me nuts. It fly's in a 3d world so x/z aren't constant. Any suggestions.


    While we're on this topic, let me tell you what I ultimately want to do. I'm using physics. I want my control (a joystick) to move the plane climb/yaw to the angle realtive to the controller. So it's not liek a flight simulator, but it's kid friendly (for guess who). Constraints aren't my answer because the joystick might be at 50% so it should only be at 50% climb angle. Plus it may be hitting uneven ground and i'd like it to flop around a little (hitting the ground on one of 3 wheels for example) which should be ok if I'm using forces as they couldn't overpower collisions. My current plan is to basically apply the force relative to the angle it's off axis by if that makes sense (the further off the more force is applied). It's a tedious way to do it but it ought to work, but I bet there's an easier way so if you know this, it might save me the trouble of figuring out the first answer.
     
  2. vagos21

    vagos21

    Joined:
    Sep 3, 2012
    Posts:
    18
    Supposing your aircraft uses its Z axis to move forward, you can compare the angle of the forward vector to the world up vector (that's the Y axis)

    you can calculate the angle between 2 vectors by the formula:

    cosθ= (a dot b)/ |a| |b|

    dot means DOT product between vectors A and B. If you want to see what dot product is, you can easily google it ;)

    and so θ=acos((a dot b)/ |a| |b|)

    since vector.forward and vector.up we'll use are unit vectors, the |a| |b| are gone so
    θ=acos(a dot b)
    This gives us the angle between the world UP axis and your aircraft.
    to get the angle between your aircraft and the horizon, simply switch cosine to sine, so finally:

    θ=asin(a dot b)

    The result for angle θ is usually given in radians by default, so if you want to convert it to degrees just use the Mathf.Rad2Deg multiplier.


    Code (csharp):
    1. for Javascript:
    2. <somewhere inside your Update function of the aircraft>
    3. .
    4. .
    5. .
    6.     var myVec:Vector3 = transform.forward; //aircraft forward vector
    7.     var worldUp:Vector3=Vector3(0.0,1.0,0.0); //world UP vector
    8.     var angle=Mathf.Asin(Vector3.Dot(myVec,worldUp)); //angle from horizon in radians
    9.     angle =  angle * Mathf.Rad2Deg; //angle in degrees
    10.     Debug.Log(angle); //let's see what we've done!
    11. .
    12. .
    13. .
    there you can see your yaw angle printed, in degrees, compared to the horizon. Positive values for climbing, negative values for descending
    Quickly tested on a camera and works here!
     
    Westland likes this.
  3. chris64

    chris64

    Joined:
    Sep 27, 2011
    Posts:
    72
    Wow, that's perfect. Thanks for your quick reponse.