Hi.
I decided to try to make an inverse kinematics script for the first time. I’m having an issue where when the Goal gets too close to the Start, the script bugs out. It looks like the end of the Arm isn’t always touching the Goal for some reason and I’m not sure why.
local Segments = {
[1] = workspace:WaitForChild("Part1"),
[2] = workspace:WaitForChild("Part2"),
[3] = workspace:WaitForChild("Part3"),
[4] = workspace:WaitForChild("Part4")
}
local Start = workspace:WaitForChild("Point1")
local End = workspace:WaitForChild("Point2")
local function GetHead(Segment) -- Gets the end of a segment
return Segment.Position + (Segment.CFrame.LookVector * (Segment.Size.Z / 2))
end
local function GetTail(Segment) -- Gets the start of a segment
return Segment.Position - (Segment.CFrame.LookVector * (Segment.Size.Z / 2))
end
local function MoveSegment(Segment, Position) -- Move a segments head to a position
Segment.CFrame = CFrame.lookAt(Segment.Position, Position)
Segment.CFrame *= CFrame.new(0, 0, -(Position - GetHead(Segment)).Magnitude)
end
local function MoveArm()
for x=1, 5 do
MoveSegment(Segments[4], End.Position)
MoveSegment(Segments[3], GetTail(Segments[4]))
MoveSegment(Segments[2], GetTail(Segments[3]))
MoveSegment(Segments[1], GetTail(Segments[2]))
-- Lock arm to start
local Offset = (GetTail(Segments[1]) - Start.Position)
for _,v in pairs(Segments) do
v.Position -= Offset
end
end
end
while wait(.2) do
MoveArm()
end