How do I make a Turret that follows the player's mouse (X and Z wise, no Y) with/without welds? I've tried multiple methods but not all of them work right.
How do I make a Turret follow the Mouse?
3 answers
This should help you out. You'll have to modify a few things and suit it to whatever you're doing. Also, keep in mind that this isn't a request site, it's a help site, so refrain from asking for such things in the future.
[LocalScript]
local part = workspace.Part -- change this to the part
local camera = workspace.CurrentCamera
game:GetService("UserInputService").InputChanged:connect(function(inputObject)
if (inputObject.UserInputType ~= Enum.UserInputType.MouseMovement) then return end
local mousePosition = inputObject.Position
local turretPosition = part.CFrame.p
local mouseRayUnit = camera:ScreenPointToRay(mousePosition.X, mousePosition.Y)
local mouseRay = Ray.new(mouseRayUnit.Origin, mouseRayUnit.Direction * 1000)
local _, mouseTarget, _ = workspace:FindPartOnRay(mouseRay)
part.CFrame = CFrame.new(turretPosition, mouseTarget)
end)
2 comments
Thanks for the help, but the problem is that the turret is attached to an unanchored ship. I should've said that in the question, but didn't. Sorry.
If it's unanchored welds would be the best choice
Problem is that a model cant have a cframe
1 comment
They can, actualy. Look up SetPrimaryPartCFrame.
That script work, All you have to do is edit the part for example
local part = workspace.["Zombie"].Barrel -- change this to the part
local camera = workspace.CurrentCamera
game:GetService("UserInputService").InputChanged:connect(function(inputObject)
if (inputObject.UserInputType ~= Enum.UserInputType.MouseMovement) then return end
local mousePosition = inputObject.Position
local turretPosition = part.CFrame.p
local mouseRayUnit = camera:ScreenPointToRay(mousePosition.X, mousePosition.Y)
local mouseRay = Ray.new(mouseRayUnit.Origin, mouseRayUnit.Direction * 1000)
local _, mouseTarget, _ = workspace:FindPartOnRay(mouseRay)
part.CFrame = CFrame.new(turretPosition, mouseTarget)
end)
The [""] is if its a model
2 comments
What he's saying is that he is using an object that's welded. I had imagined that the object was anchored.
Furthermore, you should refrain from posting an answer when you aren't actually "answering" the question.
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
Are you using FilteringEnabled?
This requires some trigonometry/unit vectors. I'll type an answer if I have time. @Programmix I don't know why you asked about filtering enabled, that wouldn't really affect this situation.
not a request site
Thanks for the help, I'll test it. However I wasn't intentionally asking it as a request, I don't see how it would be one.