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

CFrame rotation to Mouse Position?

It's probally really simple but I'm making a tank turret rotate to the mouse position.

I have this piece of code here.

Mouse.Move:connect(function()
	MouseX = Vector3.new(Mouse.Hit.x,0,Mouse.Hit.z)
	Turret.TurretWeldToHull.C0 = CFrame.new(Turret.Position,MouseX):inverse() * CFrame.new(Turret.TurretWeldToHull.Part0.Position.x, Turret.TurretWeldToHull.Part0.Position.y - 7,Turret.TurretWeldToHull.Part0.Position.z)
end)

But I have two problems.

I. The turret starts doing weird stuff whenever the mouse comes near it. I'm trying to spin the turret in one axis.

(ex. of problem) https://gyazo.com/1cbfb68bc806c63ee1bddec37aa86d0b

II. The turret is facing the wrong way! How can I fix that?

Thank you!

1 answer

It's currently set to follow the mouse's 3D position, as if the mouse was always on the ground; this explains why the turret is also looking towards the ground (especially as the mouse gets closer to the turret).

MouseX's Y coordinate should be at the exact same height as the Turret (or possibly Turret - 7, to be consistent with the second CFrame on line 3).

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