IK Walking

 avatar
MatsProg
lua
2 years ago
1.8 kB
4
Indexable
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 rightLeg = character:WaitForChild("Right Leg")
local rightFoot = character:WaitForChild("RightFoot")

local walkSpeed = 8 -- The speed at which the character will walk
local stepDistance = 4 -- The distance between steps

local walkVector = Vector3.new(1, 0, 0) -- The direction in which the character will walk
local isMoving = false

local function startWalk()
    isMoving = true
    while isMoving do
        local target = character:GetPrimaryPartCFrame() * CFrame.new(walkVector * stepDistance)
        local distanceToLeftFoot = (target.Position - leftFoot.Position).Magnitude
        local distanceToRightFoot = (target.Position - rightFoot.Position).Magnitude
        if distanceToLeftFoot < distanceToRightFoot then
            target = target * CFrame.new(-walkVector * stepDistance / 2, 0, 0)
        else
            target = target * CFrame.new(walkVector * stepDistance / 2, 0, 0)
        end
        humanoid:MoveTo(target.Position)
        humanoid.MoveToFinished:Wait()
        humanoid:Move(walkVector * walkSpeed)
    end
end

local function stopWalk()
    isMoving = false
end

humanoid.WalkSpeed = walkSpeed
humanoid.AutoRotate = false

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.W then
        startWalk()
    end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.W then
        stopWalk()
    end
end)
Editor is loading...