How do I make my arm point to the mouse's position?

Hello guys, I am working on a shooter game and I need my right arm to face the direction of the mouse. The script I have works, but when I face backward, the arm seems to go slightly back as well.

Is there any way to fix this or limit how far the arm turns?

script:

local armOffset = char.Torso.CFrame:Inverse() * (char["Right Arm"].CFrame * CFrame.new(0,0.5,-0.5))

local armWeld = Instance.new("Weld")
armWeld.Part0 = char.Torso
armWeld.Part1 = char["Right Arm"]
armWeld.Parent = char


RunService.Heartbeat:Connect(function()
	if debounce == true then
		armWeld.Parent = char.Torso
	local cframe = CFrame.new(char.Torso.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2, 0, 0)
		armWeld.C0 = armOffset * char.Torso.CFrame:toObjectSpace(cframe)
	else
		armWeld.Parent = game.Lighting
		armWeld.C0 = char.Torso["Right Shoulder"].C0 * CFrame.new(0,0,0)
	end
end)

(the gun in the video is for testing)
Thank you.

5 Likes

Use the new CFrame.lookAt instead of the deprecated CFrame.new(pos,lookAt).

You can use the look vector of the torso and compare it to the lookAtMouse CFrame using vector3:Dot(vector3), and then decide whether or not it’s facing the front/back of the torso. Here’s a helpful guide if you need one.

3 Likes