Making arm follow part/mouse

I am trying to have the top of the arm follow the target part (the target part always follows the mouse)

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local rightArm = character:WaitForChild(“Right Arm”)
local torso = character:WaitForChild(“Torso”)
local rightShoulder = torso:WaitForChild(“Right Shoulder”)
local targetPart = workspace:WaitForChild(“TargetPart”)

local function updateRightArm()
local targetPos = targetPart.Position

local targetX = targetPos.X
local torsoPos = torso.Position

local direction = Vector3.new(targetX - torsoPos.X, 0, 0).unit

local lookAt = CFrame.lookAt(torsoPos, torsoPos + direction)

local desiredC1 = torso.CFrame:ToObjectSpace(lookAt) * CFrame.new(1, 0.5, 0)

rightShoulder.C1 = desiredC1
end

game:GetService(“RunService”).RenderStepped:Connect(updateRightArm)

I’ve done just about everything and I honestly don’t know what I am doing here

Do you have another script that uses Mouse.Hit?

i do not. do I need one?

(need chars.)

I thought you did because it doesn’t reference it here, use Mouse.hit for the mouse position on 3D space and update the arm accordingly.

I have a part that follows the cursor and I am looking for it to follow that part. if that makes any sense.

Why not do:

Instead of this:

Mouse moves Part follows Mouse Arms follow Part

Doing this:

Mouse moves Part follows Mouse

Mouse moves Arms follow Mouse

Basically, make 2 functions/scripts/whatever separately and both Part and arms follow the mouse. Seems easier and should give the same result, I think?

i also did try that at first but its the same result it. it wont rotate the arm correctly.

Check this post I did some days ago. I was checking this problem for (something) following the mouse. In my case it was the whole character. It worked but not exactly how I wanted, hence the post. unfortunately I deleted the code and don’t have it, but some people commented a few solutions so you might want to check them out here

Hi CoolDude32w, I’d use basic trigonometry for this purpose, so something like this might work better;

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local rightArm = character:WaitForChild("Right Arm")
local torso = character:WaitForChild("Torso")
local rightShoulder = torso:WaitForChild("Right Shoulder")
local Mouse = player:GetMouse()

local OriginalCFrameForRightShoulder  = CFrame.new(1, 0.5, 0, 0, 0, 1, 0, 1, -0, -1, 0, 0)


local function updateRightArm()

	local RotationalPivot = rightArm.RightShoulderAttachment.WorldCFrame.Position - rightArm.CFrame.UpVector*0.5 
	local Angle = -math.asin((RotationalPivot.Y-Mouse.Hit.Y)/(RotationalPivot-Mouse.Hit.Position).magnitude)
	rightShoulder.C0 = OriginalCFrameForRightShoulder*CFrame.Angles(0,0,Angle+math.rad(90))

end

game:GetService("RunService").RenderStepped:Connect(updateRightArm)

Let me know if you have any issues, or if you want an explanation to what I’ve done here.

2 Likes

thank you this worked. I didn’t realize it was this simple.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.