How to check the angle of a ray*cast*?

Well, I pretty much want to calculate the angle of a raycast, without having to get the angle of the origin, target, etc.
Here’s a quick picture visualizing what I’d like to do:

It’s awful, I know…

1 Like

I do not really understand which angle you are trying to find. Can you provide a better visual explanation?

Just get the vector direction of the raycast then use atan to solve it. (Assuming it’s on a 2 dimensional plane)

I’m trying to do it with parts, I’ll make a better visual explanation.

You could simply use the CFrame.new constructor

local cf = CFrame.new(pos1, pos2)

Then you can use :ToEulerAngles()

print(cf:ToEulerAngles())

In that case, you subtract the vectors from each other to get your direction then you can do what I said above.

local direction = PartA.Position-PartB.Position
   Angle = math.atan(direction.Y/direction.X)
1 Like

Note that the angle will never be larger than 90 I think.

That is how you can achieve this in the 2D space.


If you want to achieve the result in the 3D space, just use the X and Z axes instead.
A code that might work:

local pos1 = (...);
local pos2 = (...);
local angle = math.atan2(pos2.Z - pos1.Z, pos2.X - pos1.X);
-- // You need to use atan2 because the angles that it returns range from -180 to 180, unlike atan which only ranges from -90 to 90.

@RatiusRat 's solution may work too.

1 Like