How to make a mouse controlled turret?

I am trying to make a turret that is controlled by mouse. The thing how could i have no idea how could i make it.

2 Likes

You need to make a base, which is the turret base. Then add a hinge constraint to the base so the turret can turn.

Next, build the actual turret. Connect the turret and the base with two attachments with the HingeConstraint. Add a welding script to the turret and make the turret a separate model from the base. Now test that your turret can spin.

Then, add a “LinearVelocity” somewhere on the turret. This velocity will allow it to direct to the users mouse position after being coded. Keep the relative to property to “World” and set the force to something that is not too high but can do the job.

Next, add a vehicleseat to anywhere on the turret. Make a script inside the vehicleseat, and inside the scripts properties, make it’s context Client.

After that, insert a serverscript into the context Client script, and a remoteevent inside the context client script.

Now that you have your scripts prepared, inside the server script, add an event to detect when the remoteevent is fired.

script.Parent.RemoteEvent.FireClient:Connect(function(MousePos)

end

Inside of the function, define the LinearVelocity.

local Velocity = Path

Then, set the LinearVelocitys VectorVelocity to the mousepos.

Velocity.VectorVelocity = MousePos

Now that that is finished, you can code the client script. Inside the client script, define the players mouse.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Next, add a function to detect when the mouse moves.

Mouse.Move:Connect(function()
script.RemoteEvent:FireServer(Mouse.Position)
end

Test it in studio.

Conclusion

If you also want the turret to go upwards, I can make explanation for that too. If there are any errors in the code then tell me, or if it does not work.

1 Like

like how can the local script work inside the seat?

i said make a normal script and make the context client. context client works anywhere

Alright. Right now we managed to make it move left and right but it will always face the mouse. NEver have like that limited agility yk.

Can you send a video? And what do you mean by limited agility?

like make it so the turret does not constantly face the mouse. Like the mouse is target and it will take a limited amount of time for the turret to face the mouse

Ohh okay, I’ll send something for limited agility

(Server) replace it with

Velocity.VectorVelocity = MousePos.Unit*2

Change the “2” to change the speed.

like i made it so there are 2 parts. THe one that will be attached to the ground and one that will rotate left and right

1 Like