How would I set a hinge contraints targetAngle from mouse position

Hey y’all,

I have a tank turret connected to the hull by a Servo type hinge constraint, and im trying to figure out how to set the Hinge Constraint’s Target Angle by the position of the mouse relative to its 3D location in the world.

anyone have any leads to how i can accomplish this?

You can get the mouse position in the worldspace with the following.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mousePos = mouse.Hit --cframe value describing mouse position
local mousePos2 = mouse.Hit.Position --vector3 value describing mouse position

im trying to figure out how to get the angle relative to its 3d position in the world.

i currently have this, which works, but only rotates the turret in each direction by about 45 degrees

part.HingeConstraint.TargetAngle =  math.deg((mouse.Origin.Position - mouse.Hit.Position  ).Unit.X)

any idea why that would only return up to 45 degrees rather than 360?

local x, y, z = CFrame.new():ToOrientation()

Call ToOrientation() through any CFrame value to receive its orientational data relative to the worldspace.

pardon me for not getting it, but how would i apply this in my situation? I tried reading up on other examples, but havent found something that helped me understand