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:
02 | player = game.Players.LocalPlayer |
03 | mouse = player:GetMouse() |
05 | findCar = game.Workspace:FindFirstChild( "Car" ..player.Name) |
07 | mainGun = findCar.Barrel |
08 | weld = findCar.VehicleSeat.Weld |
11 | mouse.Move:connect( function () |
12 | local torsoPos = findCar.TurretSwivel.Position |
13 | local dir = (mouse.Hit.p - torsoPos).unit |
14 | weld.C 0 = CFrame.new( 0 , 0 , 0 ) * CFrame.Angles(-math.atan 2 (dir.X,dir.Z) + math.pi/ 2 , 0 , 0 ) |
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.