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 7 years ago
Edited 7 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:

01wait(1)
02player = game.Players.LocalPlayer
03mouse = player:GetMouse()
04 
05findCar = game.Workspace:FindFirstChild("Car"..player.Name)
06if findCar then
07mainGun = findCar.Barrel
08weld = findCar.VehicleSeat.Weld
09 
10 
11mouse.Move:connect(function()
12        local torsoPos = findCar.TurretSwivel.Position
13        local dir = (mouse.Hit.p - torsoPos).unit
14        weld.C0 = CFrame.new(0,0,0) * CFrame.Angles(-math.atan2(dir.X,dir.Z) + math.pi/2, 0, 0)
15end)
16end

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 6 years ago

If you mean something like a rotating loop, try:

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

Answer this question