Search Unity

Unity 360 degree

Discussion in 'Scripting' started by Rutvik, Jul 3, 2015.

  1. Rutvik

    Rutvik

    Joined:
    May 30, 2015
    Posts:
    8
    I want to find angle between two vectors (or three points). There is a built in function Vector3.Angle but it only gives angle between -180 to +180. So How can I get 360 degree angle between two vectors?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    the range of -180 to 180 is 360 degrees

    A -1 degree means 1 degree clockwise, or 359 degrees counter-clockwise.

    0 and 360 are equal.

    It comes back in that range because the trig functions used to calculate angles stay in the range of -pi to pi. And pi radians is 180 degrees.


    If you want to normalize your angle in a 0 -> 360 range, you add 360 to the value until it's positive, and then modulo around 360.

    Code (csharp):
    1.  
    2. public static float NormalizeAngleTo360(float angle)
    3. {
    4.     while(angle < 0f) angle += 360f;
    5.     return angle % 360f;
    6. }
    7.