How to get an angle from cursor position?

You can calculate the angle using CFrames as well.

Here is how @ThanksRoBama calculate the yaw angle, or y axis rotation for his turret.

Visualization:

	local object_horizontal_offset = yawBase.CFrame:PointToObjectSpace(target.Position)
	local object_yaw_angle = math.atan2(-object_horizontal_offset.X, -object_horizontal_offset.Z)
--Yaw angle is the goal CFrame
--Next is PID stuff which I believe you are not concerned with
--yawBase is the base of the turret.
--I heavily suggest experimentation by printing out the values or
--some other value monitoring method to make sure you get the right angle
print(object_yaw_angle)

Edit: Yep here is how it works by measuring the horizontal yaw angle relative to the parts look vector, a reminder that this angle is relative to yawBase CFrame position and orientation.

5 Likes