Need help with arm following the mouse

  1. What do you want to achieve?
    I want my arm to point at the mouse cursor.
  2. What is the issue?
    Well I am using the script below and this is what happens:

I copied the script from a head following camera tutorial and tried changing some variables so the arm will follow the mouse. But it doesnt quite work.

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character or player.CharacterAdded:Wait()
local humanoidrootpart = character:FindFirstChild("HumanoidRootPart")
local Arm = character:FindFirstChild("RightShoulder", true)
local elbow = character:FindFirstChild("RightElbow", true)
local yOffset = Arm.C0.Y
local xOffset = Arm.C0.X
local zOffset = Arm.C0.Z

game:GetService("RunService").RenderStepped:Connect(function()
	local camdirection = mouse.Hit.LookVector
	if Arm then
		Arm.C0 = CFrame.new(xOffset,yOffset,0) * CFrame.Angles(0, -math.asin(camdirection.x),0) * CFrame.Angles(math.asin(camdirection.y),0,0)
	end
end)

When you set Arm.C0 near the last line, could you try adding * CFrame.Angles(math.pi/2, 0, 0) to the end? This should rotate the arm 90 degrees so it points forward instead of down. If it makes the arm point in the opposite direction, try making math.pi/2 negative.

Edit: Use CFrame.Angles, not CFrame.new!

1 Like

Wow It works! Thanks a lot, I really dont know how people can be that good at programming :wink:

1 Like

Nvm, sorry. When I move the camera then it stops working :frowning:

1 Like

I’m testing this in Studio currently, are you talking about how the arm can’t point behind the character?

1 Like

No, its at first it works fine, but when I move and turn to the right for example (changing cmaera position) then it doesnt really work.

1 Like

Here is a short clip of what I mean, when I turn around then the pointing thing inverses.

Should I use the CFrame of the mouse relative to the HumanoidRootPart?

1 Like

That’s what I just tried, it looks like it works.

game:GetService("RunService").RenderStepped:Connect(function()
	local camdirection = mouse.Hit.LookVector

	local relcamdirection = humanoidrootpart.CFrame.Rotation:PointToObjectSpace(camdirection)
	if Arm then
		Arm.C0 = CFrame.new(xOffset, yOffset, 0)
			* CFrame.Angles(0, -math.asin(relcamdirection.x), 0) 
			* CFrame.Angles(math.asin(relcamdirection.y), 0, 0)
			* CFrame.Angles(math.pi/2, 0, 0)
	end
end)
4 Likes

Whoa, that is complicated but it works, thanks!

1 Like