I've been trying to code for inverse kinematics, which was initially a success before trying to make it fixed point. My way of making it fixed point was to offset all the limbs by the difference between the starting position and the end of the first joint. This works when the area it is reaching for is farther than the length of the limb, but when it is within the region that it can reach, it begins infinitely rotating, as the end joint will always be slightly offset from the needed position.
I've tried checking to see if I can use if statements, like seeing if it's within 0.1 studs of the goal, or the rotation is roughly the same as the next rotation, but this only fixed it in some scenarios.
Anybody know how I can fix this issue, to either make it find a completely correct solution first try or to just stop it rotating when it's 99% correct?
If it helps, here is the snippets of the code that actually moves each segment:
function moveSegment(currentPosition,targetPosition,segment) segment.CFrame = CFrame.new(currentPosition,targetPosition) segment.Position = targetPosition-segment.CFrame.LookVector*length/2 end
local target = script.Parent.End.Position target = script.Parent.End.Position for i,v in pairs(segments) do moveSegment(v.Position,target,v) target=v.Position-v.CFrame.LookVector*0.5*length end local difference = script.Parent.Start.Position-(segments[#segments].Position-segments[#segments].CFrame.LookVector*length/2) for i,v in pairs(segments) do v.Position+=difference end