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

finding an angle between two points

Discussion in 'Scripting' started by Marscaleb, Aug 11, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    973
    I'm working on an AI for an enemy, but there are some math bits that I do not know how to execute.

    I need to find the angle between the enemy and the player, so that I can attack the player (or rather where the player is at that exact moment.)
    I only need the angle in 2D space, and the code I am using to accelerate to the player's location requires the angle in radians. It's easy to convert degrees to radians, but either way, I'd need an angle between two points in X and Y.

    How do I calculate that angle?
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    I think Mathf.Atan2 is going to be more in line with what you need than Vector2.Angle, given that V2.Angle will only ever give a positive number between 0 and 180 (it's the difference between two direction vectors) with no indication of which direction the vector is rotating. Atan2, on the other hand, gives you a full 360 degree exact angle - or rather, it gives you a full 6.28 exact angle, because conveniently enough (for you), it returns the result in radians!
     
    fiknikos and DoomSamurai like this.
  4. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    973
    But Atan2 requires two floats, and I have two positions.
     
    Galse22 likes this.
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    You use y2-y1 and x2-x1.
     
  6. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    973
    Okay! It's working now, thanks!

    Vector2.angle was making it fly off in the same direction every time.
     
  7. DawsonMatthews

    DawsonMatthews

    Joined:
    Aug 26, 2020
    Posts:
    1
    Been struggling with the same thing, just want to say that this was the solution for me, thanks.
     
  8. Born2BCoder

    Born2BCoder

    Joined:
    Aug 8, 2013
    Posts:
    1
    simple, use this

    Vector2 Point_1 = new Vector2([pos_x],[pos_y]);
    Vector2 Point_2 = new Vector2([pos_x],[pos_y]);
    float angle = Mathf.Atan2(Point_2.y - Point_1.y , Point_2.x-Point_1.x) * 180 / Mathf.PI;

    Good Luck
     
    mentalup, xBones, Erishno and 4 others like this.
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    Please note that this is an old post you're replying to.

    You simply use Vector2.Angle or Vector2.SignedAngle. It should take moments to see this in the API docs.
     
  10. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    973
    Or you could replace the * 180 / Mathf.PI with a simpler * mathf.Rad2Deg.

    @MelvMay that Vector2.Angle simply never works right; it never gives me a proper angle in degrees. I don't know what is wrong with it. The Atan2 method is the only thing I can get tow work right.
    Side note, that avatar is the greatest I have ever seen.
     
    prohit_unity likes this.
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,472
    It's simply because there is no such thing as an angle between two points. You can only have an angle between two lines and those require 3 points. Vector2.Angle calculates the angle at the origin between the two lines from the origin to each of the two points. Such points are actually known as vectors. All vectors have their tail sitting at the origin.

    You don't want the angle between two points as seen from the origin, you want the angle of the line between your two points to some reference line. That reference line is mathematically usually the x axis. That's what Atan2 is actually calculating because angles in math start with 0° at the x axis. So to get the angle of the line between your two points in reference to the x-axis you could do:

    Code (CSharp):
    1. float angle = Vector2.Angle(p2 - p1, Vector2.right);
    or if you need a signed angle you can use Vector2.SignedAngle in the same way:

    Code (CSharp):
    1. float angle = Vector2.SignedAngle(p2 - p1, Vector2.right);
    If you have trouble understanding the concept of vectors vs points (points are just vectors from the origin),I can generally recommend the 3B1B series on linear algebra. If you have trouble with trigonometry and how to apply vector math to specific game development issues, I can recommend Freya Holmer's series Math for Game Devs. Those are quite verbose videos, so watch them if you really want to learn how it works. Yes, it takes some time but game dev requires a lot of math and you should get comfortable with it, at least with the basic stuff.
     
    Key-Djar, IndieForger and Marscaleb like this.
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    It makes me smile every day! :)
     
    Lurking-Ninja and Bunny83 like this.
  13. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    973
    Today is a good day to smile!
     
    MelvMay likes this.
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    Awesome! Well there you go, I stole that for my forum signature. :p
     
    Marscaleb likes this.
  15. JohngUK

    JohngUK

    Joined:
    Aug 8, 2019
    Posts:
    11
    Great thread: suddenly the wonderful world of vectors starts to make sense!