Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a Turret follow the Mouse?

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.

4 comments

0

Are you using FilteringEnabled?

0

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.

0

not a request site

0

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.

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

0

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.

0

If it's unanchored welds would be the best choice

Problem is that a model cant have a cframe

1 comment

0

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

0

What he's saying is that he is using an object that's welded. I had imagined that the object was anchored.

0

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.