Angle between two Vector2s

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I need to know the angle between two 2D unit vectors, something like Unity’s Vector2.Angle

  2. What is the issue? I need to be able to determine if an object is within a certain radius and field of view of another object, where I need the angle between the forward direction (normalized) and the direction to the target (normalized).

  3. What solutions have you tried so far? I have looked for solutions, but none of them seem to match what I need

What I’m trying to do, is something like this:

if Angle(forward, dirToFood) < viewAngle / 2 then

I already have the radius figured out.

It might help if you draw a triangle to visualize how to do this.


If you have two points then you can form a right triangle and it becomes obvious that the angle between them is just math.atan(dy / dx) where dy = p1.Y - p2.Y and dx = p1.X - p2.X

However you might want to use math.atan2 because it compensates for quadrants where dx or dy could be negative.

math.atan2(dy, dx)
1 Like

This explanation seems sufficiently in-depth. Best way to get the angle between two vectors? - #4 by ThanksRoBama

1 Like

There’s a mathematical formula that gives you that:

function Angle(a, b)
    return math.atan2(b.Y - a.Y, b.X - a.X)

I love how you guys are confusing him when literally the dot product of 2 unit vectors is the ratio between them. Just do this

local function Angle(a1, a2)
  return math.rad(math.acos(a1.Unit:Dot(a2.Unit)))
1 Like