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

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

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.