How can I find angles between two vectors on 2D plane in 3D space?

I have both parts. Both of them have the same directions which is lookvector direction using CFrame. These lookvectors is positive. I compare two lookvectors from both parts to find the angle of 2 lookvector and the result will look like this:


They’re both are parallel. The output angle will be 0.
Output:
Angle: 0

Perpendicular lookvector directions:


Output:
Angle: 90

I added Vector3.xAxis to get the negative 90.
local Angle = LookVectorB:Angle(LookVectorA,Vector3.xAxis)


Output:
Angle: -90

It still calculates angles when I rotate the Y axis.


Angle:90

I tried to eliminate the X vector by multiplying the direction of LookVectorB with Vector3 like this:
local LookVectorB = PartB.CFrame.LookVector * Vector3.new(0,1,1)
So when I rotate on Y axis, It prevents it from calculating angles. This will output the angle to 0.
The issue is when I rotated the Y axis pass 90, the angle became 180 degrees. I want the angle to always remain 0 when I rotate the Y axis. I don’t really know how to fix this issue. This is the code:

local RunService = game:GetService("RunService")
local PartA = workspace.PartA
local PartB = workspace.PartB
RunService.Stepped:Connect(function()
    local LookVectorA = PartA.CFrame.LookVector
    local LookVectorB = PartB.CFrame.LookVector * Vector3.new(0,1,1)
    local Angle = LookVectorB:Angle(LookVectorA,Vector3.xAxis)
    print("Angles: ", math.round(math.deg(Angle)*1000)/1000)
end)
1 Like

I’m not certain of what you’re going for here, but you could try zeroing/removing the Y factor from the situation if that’s what you’re attempting to do.

LookVectorB gets rid of the X factor but introduces the error for the “Y issue” you’re having I would believe. You should have LookVectorB set as PartB.CFrame.LookVector and go about Subtracting the Y angle from it.

1 Like