IK Leg

 avatar
MatsProg
lua
a year ago
932 B
1
Indexable
Never
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local leftLeg = character:WaitForChild("Left Leg")
local leftFoot = character:WaitForChild("LeftFoot")

local target = workspace.Target -- the part or position you want the leg to step towards

local lengthUpperLeg = leftLeg.UpperLegLength
local lengthLowerLeg = leftLeg.LowerLegLength

while true do
    local distance = (target.Position - leftFoot.Position).Magnitude
    if distance > lengthUpperLeg + lengthLowerLeg then
        local targetPosition = target.Position - leftLeg.CFrame:VectorToWorldSpace(Vector3.new(0, lengthUpperLeg, 0))
        humanoid:MoveTo(targetPosition)
    else
        local targetRotation = CFrame.lookAt(leftFoot.Position, target.Position)
        leftLeg.CFrame = targetRotation * CFrame.new(0, -lengthUpperLeg, 0)
    end
    wait()
end