Need help with making a turret follow the player's mouse! [STILL NEED HELP!]

Hey everyone! I am trying to make a turret follow the mouse with Welds as I am using a moving vehicle however when I move my mouse the turret spins and glitches.

I have looked for a few solutions so far and I have thought of doing a hinge constraint servo however when somebody drives the vehicle, the hinge follows the rotation of the vehicle which is not what I want!

The method I am using right now uses the math.atan2 function to fetch the yaw angles based on the turret base.

Below is my code that I have tried out so far:

local object_horizontal_offset = Sentry.Parent.Base.CFrame:PointToObjectSpace(mouse.Hit.Position)
local object_yaw_angle = math.atan2(-object_horizontal_offset.X, -object_horizontal_offset.Z)
Sentry.Parent.Base.Weld.C1 = CFrame.new(-0.3, 2, -10) * CFrame.Angles(0, object_yaw_angle, 0)

And it follows the mouse but it spins and I don’t know if it is to do with the welds on the base?
Demonstration here: 2022-12-09 21-43-36

Thank you and I appreciate any help as I am fairly new to this approach with CFrames. If there is a better way then be sure to let me know!

I’m really bad at this, but couldn’t you use CFrame.lookAt?

1 Like

Unfortunately it will not work for my vehicle as it is attached to a car but it’s a good approach if I don’t have a moving vehicle!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.