Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make a Turret follow the Mouse?

Asked by 8 years ago

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.

0
Are you using FilteringEnabled? Programmix 285 — 8y
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. Legojoker 345 — 8y
0
not a request site lukeb50 631 — 8y
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. RivaiIle 5 — 8y

3 answers

Log in to vote
1
Answered by 8 years ago

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)
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. RivaiIle 5 — 8y
0
If it's unanchored welds would be the best choice YellowoTide 1992 — 8y
Ad
Log in to vote
0
Answered by 7 years ago

Problem is that a model cant have a cframe

0
They can, actualy. Look up SetPrimaryPartCFrame. RubenKan 3615 — 7y
Log in to vote
-1
Answered by 8 years ago

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

0
What he's saying is that he is using an object that's welded. I had imagined that the object was anchored. Programmix 285 — 8y
0
Furthermore, you should refrain from posting an answer when you aren't actually "answering" the question. Programmix 285 — 8y

Answer this question