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: