Search Unity

Global heading of an object

Discussion in 'Scripting' started by mtompson-com, Nov 22, 2015.

  1. mtompson-com

    mtompson-com

    Joined:
    Feb 4, 2013
    Posts:
    15
    hi
    I need an objects global heading not it's local heading or transform.forward.

    Imagine an aircraft having to yaw into wind as it flies forward.

    It's the same kind of heading a GPS gives as it looks at the angle from to consecutive points. In a boat it's known as COG ( course over ground ) and is not the direction the boat is necessarily facing.

    Anyway, if I've made any sense, I'd appreciate some help, thanks.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    are you using a rigidbody?
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Draw us a picture?
     
  4. mtompson-com

    mtompson-com

    Joined:
    Feb 4, 2013
    Posts:
    15
    LeftyRighty, yes I am, that's how I manage this movement, so an add force from the side. And BoredMormon, I'll draw a picture asap.
    Thanks
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you are using a rigidbody do you simply want to read rigidbody.velocity? You can normalize it if you need a unit vector.
     
    LeftyRighty likes this.
  6. mtompson-com

    mtompson-com

    Joined:
    Feb 4, 2013
    Posts:
    15
    looks good, thanks BoredMormon, so I'm trying:
    Code (CSharp):
    1. float angle = (Mathf.Atan2(transform.rigidbody.velocity.x,transform.rigidbody.velocity.z) * Mathf.Rad2Deg);
    2.  
    3.             angle = (angle + 360f) % 360f;
    looks good so far.
     
  7. mtompson-com

    mtompson-com

    Joined:
    Feb 4, 2013
    Posts:
    15
    Just to add if anyone needs this kind of thing, the SOG ( speed over ground ) is 'transform.rigidbody.velocity.magnitude' but my forward (local) speed ( which is the same as SOG if there are no side forces) is:
    Code (CSharp):
    1. SPD = transform.rigidbody.velocity - sideForceVector;
    2. SPD.magnitude;
    3. //test
    4. Debug.DrawRay (transform.position, SPD, Color.red);