Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why am I getting the wrong angles with Vector2.Angle?

Discussion in 'Scripting' started by Paparakas, Nov 7, 2013.

  1. Paparakas

    Paparakas

    Joined:
    Feb 28, 2013
    Posts:
    90
    I've googled it but I haven't found an answer that fits my situation.

    I'm trying to simply get the angle between two objects. Googling told me that it won't work if I use the transform.position, so I just inputted hard values into 2 vector 2Ds, which have the same x and y value as the objects they correspond to.

    So to test out angles between two points on paper to check it against, I did this:

    $CYKa4Sr.png

    If I do:

    Code (csharp):
    1. Vector2 A = new Vector2(6,3);
    2.         Vector2 B = new Vector2(8,5);
    3.  
    4.         Vector2 C = B - A;
    5.         float Angle = Mathf.Atan2(C.y,C.x);
    6.         float AngleInDegrees = Angle * Mathf.Rad2Deg;
    7.         Debug.Log("Atan2 Angle in degrees " + AngleInDegrees);
    I'll get the correct answer, which is 45 degrees. But if I then do this:

    Code (csharp):
    1. AngleInDegrees = Vector2.Angle(A,B);
    2.         Debug.Log("Vector2.Angle in degrees is " + AngleInDegrees);
    3.  
    I'll get 5.440317, which is not correct. I know that I'm the one doing something wrong, and that it's correct given what values I put in the parameters, but why exactly is it giving me this number, and not 45 degrees?
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    Vectors can be interpreted either as a line from the origin or as a point in space. You're following the latter interpretation and expecting it to give you the angle from X of the line between the two points. Unity's Vector3.Angle() method follows the former interpretation and calculates the angle between the two lines. That is, if you drew lines on your diagram from (0,0) to each of your points, Unity is giving you the angle between those two lines.
     
    c4rl0s00 and Tabbyz like this.
  3. Paparakas

    Paparakas

    Joined:
    Feb 28, 2013
    Posts:
    90
    I see, thanks for the answer! So something like this?

    $hHNxKrz.png


    What uses does such an angle have in unity?
     

    Attached Files:

    Last edited: Nov 7, 2013
    Tabbyz likes this.
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    Yeah, something like that.

    It's used quite a lot, like when seeing how far something needs to turn to point towards something else or calculating the intensity of an effect that depends on angles.
     
    bathingsoap likes this.
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    If you want to use Vector2.angle like that, you need to give it AB and some other vector, to get the angle between them. Usually when we do this, its one of the cardinal axes that we want (0,1) or (1,0)

    if you try using AB and (1,0) it should give you 45
     
  6. Mecanamaral

    Mecanamaral

    Joined:
    Sep 24, 2020
    Posts:
    7
    Bugged method, my region use decimal separator as "," and not ".", for any reason it getting this number format from my windows and applying in this method, so the vector2 is considered as a quaternion: (1,0,1,0) real being hard to debug those "unthinking mistress in unity", like hours as a retard to understand why the f*** method always return 0 degrees

    By mathf.atan2 I got it, not the same bug, but the description is wrong, it is x, y and not y, x.... Thank you Paparakas
     
    Last edited: Nov 19, 2020
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    What has the string representation of numbers in different regional cultures to do with calculating the angle between two vectors? You bringing up completely unrelated things into this thread. Why don't we talk a bit about me vaccum cleaner -.-

    About your second point, no Mathf.Atan2 is absolutely correctly named. The parameters are y, x not x, y. Atan2 is just a quadrant aware version of the normal Atan method. That's all just pure mathematical. The normal Atan is used like this:
    Atan(y / x)

    The tangent is always the opposite divided by the adjacent. Since the angle we are interested in is the one at the origin, the opposite is the y component and the adjacent is the x component. In order to figure out the correct quadrant the Atan2 method needs to know the signs of the two values, so you can not do the division first as the signs would be lost / combined that way. That's why Atan2 wants the two values seperately and it does the division internally.

    There are always people complaining that the Atan2 parameters are the wrong way around, however it's usually based on the fact that you don't realise what reference is used for the angle. In mathematics it's always the positive x axis. So a Vector that points to the right is considered 0°. A Vector pointing up in the positive y direction is 90°. See the wikipedia article on the inverse trigonometric functions.