How to get an angle from cursor position?

So I am making a system of rotating where your cursor points, the game will rotate the turret to direction that mouse is at.
I know I could do this with CFrame but I would prefer finding the angle that I can apply to a hinge more than CFrame

2 Likes

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.

3 Likes

is there a way to make it follow mouse without having you to click though

1 Like

Yep, just use a constant loop to make it follow the mouse 3d world position preferable RunService Heartbeat since normally it runs faster and hence more responsive than a while true do wait() loop.

1 Like

Yeah I’m already running a while loop but I can’t pass mouse position without using Mouse.Hit

1 Like

Why not use mouse.Hit.Position, what are the problems are you facing?

Edit: There is also Camera | Roblox Creator Documentation to convert 2d cursor position to 3d world space which your turret is located I believe when you mentioned hinge. Is this what you were looking for?

1 Like

I just don’t want the player have to click to rotate to the direction but rather rotate with their mouse as it moves

1 Like

Nevermind I’m just stupid and thought Mouse.Hit responded for when mouse was clicked

3 Likes

Is there any way to do the same thing but with Y-axis?