Weird Scripted Inverse Kinematics Behavior

I asked Chat GPT to try to spit some code out for some simple Inverse Kinematics, my goal was to make the players left arm follow an attachment on the steering wheel as it’s steering, and after multiple attempts I got something that kind of worked:

As you can see in the video it kind of works but has this weird offset based on movement and rotation from the start position, I tried to get Chat GPT to fix its own code but it being what it is, it did not know how.

I myself am not the sharpest tool in the shed so if someone good at the maths behind it could help me understand/fix what is going on here that would be greatly appreciated:

Code:

local LeftArm = Character:WaitForChild("Left Arm")
local torso = Character:WaitForChild("Torso")

-- Assume targetAttachment is a part that represents the target position for the arm
local targetAttachment = SteeringWheelPart.Attachment1

-- Get the positions for calculations
local shoulderPosition = torso.Position + Vector3.new(1.5, 0, 0)  -- Adjust the shoulder offset
local targetPosition = targetAttachment.WorldPosition  -- Get the world position of the target attachment

-- Calculate the direction vector from the shoulder to the target
local shoulderToTarget = (targetPosition - shoulderPosition).unit  -- Direction vector

-- Ensure that the arm rotates towards the target position
local upperArmDirection = LeftArm.CFrame.LookVector  -- The current direction of the arm

-- Calculate the required rotation using CFrame
local targetCFrame = CFrame.lookAt(shoulderPosition, targetPosition)  -- Directly calculate the desired orientation

-- Apply the rotation to the Motor6D's C0 property
local motor = torso:FindFirstChild("Left Shoulder")  -- The Motor6D that connects the upper arm to the torso
if motor then
	-- Adjust the C0 of the Motor6D to make the arm point towards the target
	motor.C0 = motor.C0:lerp(targetCFrame, 0.1)  -- Smooth interpolation between current and target orientation
end

The code yields no errors

The attachment it is trying to reach is inside of the steering wheel itself:
SteeringWheel2

I think you’re complicating this way too much.

You can use animations to make it look like the arm is turning the wheel.

1 Like

There are many, many reasons I’m using inverse kinematics over animation, if I was to list them all we would be here a while so I’m going to say the biggest one is flexibility. Inside of my car system are modules where information about how the cars handling is stored, one of those packets of data contains the steering wheel speed, smoothing, and steering wheel turn degrees. There is then code that will take this information and animate via script the wheel turning left/right to follow the car wheels, using inverse kinematics I wouldn’t need to make different animations for different vehicles depending on the amount that the steering wheel turns, or speed of players steering based on skill points.

I have instead decided to use a well known module made by @robert_stevey already handy for this purpose instead of writing my own code:

Thanks for the assistance.

1 Like

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