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

How do you make a part point towards your mouse along the y axis by CFraming it?

Asked by 6 years ago
Edited 6 years ago

I have two parts that are held together by a weld. They're called TurretSwivel and Turret. Turret is the part that moves. I need to CFrame the weld by using something similar to the code below.

This is an example of what I have tried:

wait(1)
player = game.Players.LocalPlayer
mouse = player:GetMouse()

findCar = game.Workspace:FindFirstChild("Car"..player.Name)
if findCar then
mainGun = findCar.Barrel
weld = findCar.VehicleSeat.Weld


mouse.Move:connect(function()
        local torsoPos = findCar.TurretSwivel.Position
        local dir = (mouse.Hit.p - torsoPos).unit
        weld.C0 = CFrame.new(0,0,0) * CFrame.Angles(-math.atan2(dir.X,dir.Z) + math.pi/2, 0, 0)
end)
end

When I use this script, it works at first but soon the turret stops aiming at the mouse.

Please help answer this, thanks. And if you have any questions please ask.

1 answer

Log in to vote
0
Answered by 5 years ago

If you mean something like a rotating loop, try:


local part = [Part you want to rotate] local mousePos = mouse.Hit.p weld.C0 = part.CFrame:inverse() * CFrame.new(part.Position, Vector3.new(mousePos.x, part.Position.y, mousePos.z)
Ad

Answer this question