Inverse Kinematics (Pointing Arm) Issue while walking

Hey guys! I hope you all do very very well! Being direct, I was testing a script of inverse kinematics, but I got a problem, and It’s that when the player is starting to walk around the arm doesnt point ot the target anymore:

-Here’s the video:

I don’t really know how to fix this, and I’ll be VERY grateful with you and your heart if you can give me a hand with this, I don’t really know how to fix this, thanks for reading and have a nice day! :wink:

My goal is to make the arm move with inverse kinematics while the player/character is moving.

Heres the script (ModuleScript/Client)

local IK = {}

local function solveArm(originCFrame,targetPosition,a,b)
	local localized = originCFrame:pointToObjectSpace(targetPosition)
	local localizedUnit = localized.unit
	local l3 = localized.magnitude
	
	local axis = Vector3.new(0, 0, -1):Cross(localizedUnit)
	local angle = math.acos(-localizedUnit.Z)
	local planeCF = originCFrame * CFrame.fromAxisAngle(axis, angle)
	
	if l3 < math.max(b, a) - math.min(b, a) then
		return planeCF * CFrame.new(0, 0,  math.max(b, a) - math.min(b, a) - l3), -math.pi/2, math.pi
	elseif l3 > a + b then
		return planeCF * CFrame.new(0, 0, a + b - l3), math.pi/2, 0
	else
		local a1 = -math.acos((-(b * b) + (a * a) + (l3 * l3)) / (2 * a * l3))
		local a2 = math.acos(((b  * b) - (a * a) + (l3 * l3)) / (2 * b * l3))

		return planeCF, a1 + math.pi/2, a2 - a1
	end
end

function IK.BuildPacketArms(character)
	local packet = {}
	packet.character = character
	packet.upperTorso = character:WaitForChild("UpperTorso")
	packet.rightUpperArm = character:WaitForChild("RightUpperArm")
	packet.rightLowerArm = character:WaitForChild("RightLowerArm")
	packet.rightShoulder = packet.rightUpperArm:WaitForChild("RightShoulder")
	packet.rightElbow = packet.rightLowerArm:WaitForChild("RightElbow")
	packet.rightShoulderInit = packet.rightShoulder.C0
	packet.rightElbowInit = packet.rightElbow.C0
	packet.rightWrist = character:WaitForChild("RightHand"):WaitForChild("RightWrist")
	return packet
end

function IK.Arms(packet, target)
	local SHOULDER_C0_CACHE	= packet.rightShoulder.C0
	local ELBOW_C0_CACHE = packet.rightElbow.C0

	local UPPER_LENGTH = math.abs(packet.rightShoulder.C1.Y) + math.abs(packet.rightElbow.C0.Y)
	local LOWER_LENGTH = math.abs(packet.rightElbow.C1.Y) + math.abs(packet.rightWrist.C0.Y) + math.abs(packet.rightWrist.C1.Y)
	local shoulderCFrame = packet.upperTorso.CFrame * SHOULDER_C0_CACHE
	local goalPosition = target.Position	
	local planeCF, shoulderAngle, elbowAngle = solveArm(shoulderCFrame, goalPosition, UPPER_LENGTH, LOWER_LENGTH)

	local goalRightShoulderC0CFrame = packet.upperTorso.CFrame:toObjectSpace(planeCF) * CFrame.Angles(shoulderAngle, 0, 0)
	packet.rightShoulder.C0 = goalRightShoulderC0CFrame.Rotation + packet.rightShoulder.C0.Position
	packet.rightElbow.C0 = packet.rightElbowInit * CFrame.Angles(elbowAngle, 0, 0)
end

return IK

Even if you can give me the smallest hand, I’ll be grateful with it, thank you and have a nice day

1 Like

Maybe it’s getting overshadowed by the run animation. An idea I have is to make the arm that is supposed to point to the target invisible and have a clone that isn’t affected point to the target.

You’re mixing two things:

Inverse Kinematics (IK) - which uses joint position and orientation to predict the position of the end effector
Forward Kinematics (FK) - which uses joint position and orientation to predict the position of the end effector

The first one is done by the script you posted. The second one is done by the actual joints in the model. You are using the IK script to set the joint positions and orientations directly, which is why the joints don’t move anymore when the player starts walking.
To fix this, you have to either:

Use the script you posted to calculate the right positions and orientations and then apply them to the joints using the SetHumanoidJointTarget function. This will cause the joints to move, but it will also cause the joints to stop moving when the player stops moving.
Use the script you posted and then use the positions and orientations calculated by the script to move the model itself, not the joints. This will cause the model to move, but not the joints.

If you want to continue with option 1, you can refer to this post:

How to make a character arm follow mouse position

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