How would I find the angle between two vector3s?
✓ ANSWER ACCEPTED
Angle between two vectors3s?
2 answers
✓ Accepted answer
RubenKan explained how to get the direction between two vectors, but if you're actually looking for the angle between them you want the dot product.
local function angleBetween(a, b)
return math.acos(a:Dot(b) / (a.magnitude * b.magnitude));
end;
Keep in mind this angle will be between 0 and 180 degrees.
According to this example, I created a function for you that calculates the angle (in degrees) between two Vector3 variables.
-- Returns angle between two vectors (in degrees)
function angleBetween(v1, v2)
return math.deg(math.acos(v1:Dot(v2)/(v1.Magnitude*v2.Magnitude)))
end
-- Use like this
local angle = angleBetween(Vector3.new(2,9,-3), Vector3.new(-3,-4,8))
print(angle)
2 comments
0
Thanks for the help! I believe EgoMoose’s post has a comment that utilises Roblox’s own functions.
0
EgoMoose's code is no different from mine, although BlueTaslem did propose a shortcut by using the unit vectors from the get-go.
This discussion is preserved as it was. New answers, comments, and votes are closed.

6 comments
More succinctly,
math.acos(a.Unit:Dot(b.Unit))Thanks. Is there any way I can use this to return an angle between 0 and 360 degrees?
Yes, you need to use the 'math.deg()' function to get the answer back in degrees (and not radians). See my original post's function, which does this. The answer will always be between 0 and 180, because the range of arc cosine is [0, pi]. See here: http://www.analyzemath.com/Graphing/graphing_arccosine.html
Thanks - no way to detect if the part is behind the other part, then?
I don't see how that relates to this question?
Sorry for the late response, I had to go let my cat out. I was just wondering if there was a workaround for the 0–180 degrees lock. I wanted a full 360 degrees range to check if a part was behind another part, though now realise there's better ways of doing this. Thanks again.