4 Way Directional Walking System Open Sourced

This is a 4 way directional walking system for R6, make sure to not leave the id’s empty or it might not work, here is the place file:
Baseplate.rbxl (43.1 KB)
Here is the code:

local animationFolder = game:GetService("ReplicatedStorage"):FindFirstChild("Animations")

if not animationFolder then
	error("Animations folder not found")
end

local walkForwardAnim = animationFolder:FindFirstChild("WalkForwardR6")
local walkBackwardAnim = animationFolder:FindFirstChild("WalkBackwardR6")
local walkLeftAnim = animationFolder:FindFirstChild("WalkLeftR6")
local walkRightAnim = animationFolder:FindFirstChild("WalkRightR6")

if not walkForwardAnim or not walkBackwardAnim or not walkLeftAnim or not walkRightAnim then
	error("One or more walk animations not found")
end

local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local animator = humanoid.Animator

local animationControllers = {}

local function playAnimation(animation)
	local animationController = animationControllers[animation.Name]

	if not animationController then
		animationController = animator:LoadAnimation(animation)
		animationControllers[animation.Name] = animationController
	end

	if animationController.IsPlaying then
		return animationController
	end

	animationController:Play()
	animationController:GetMarkerReachedSignal("End"):Connect(function()
		animationController:Stop()
	end)

	return animationController
end

local function stopAnimation(animationController)
	if animationController then
		animationController:Stop()
	end
end

local function getMoveDirection()
	local rootPart = humanoid.RootPart
	local forwardDirection = rootPart.CFrame.LookVector
	local rightDirection = rootPart.CFrame.RightVector

	local moveDirection = forwardDirection * humanoid.MoveDirection.Z + rightDirection * humanoid.MoveDirection.X

	return moveDirection.Unit
end


local function applyMotionR6(rootPart, moveDirection)
	local x = moveDirection.X
	local y = moveDirection.Y
	local z = moveDirection.Z

	if y > 0 then
		rootPart.Velocity = Vector3.new(x * 16, y * 40, z * 16)
	else
		rootPart.Velocity = Vector3.new(x * 16, 0, z * 16)
	end

	rootPart.CFrame = rootPart.CFrame + Vector3.new(x * 16, 0, z * 16)
end

game:GetService("RunService").Heartbeat:Connect(function()
	local moveDirection = getMoveDirection()

	if moveDirection.Magnitude > 0 then
		humanoid.WalkSpeed = 16

		local dotForward = moveDirection:Dot(Vector3.new(0, 0, -1))
		local dotBackward = moveDirection:Dot(Vector3.new(0, 0, 1))
		local dotLeft = moveDirection:Dot(Vector3.new(-1, 0, 0))
		local dotRight = moveDirection:Dot(Vector3.new(1, 0, 0))

		local currentAnimationController = nil

		if dotForward > 0.75 then
			stopAnimation(animationControllers["WalkBackwardR6"])
			stopAnimation(animationControllers["WalkLeftR6"])
			stopAnimation(animationControllers["WalkRightR6"])
			currentAnimationController = playAnimation(walkForwardAnim)
		elseif dotBackward > 0.75 then
			stopAnimation(animationControllers["WalkForwardR6"])
			stopAnimation(animationControllers["WalkLeftR6"])
			stopAnimation(animationControllers["WalkRightR6"])
			currentAnimationController = playAnimation(walkBackwardAnim)
		elseif dotLeft > 0.75 then
			stopAnimation(animationControllers["WalkForwardR6"])
			stopAnimation(animationControllers["WalkBackwardR6"])
			stopAnimation(animationControllers["WalkRightR6"])
			currentAnimationController = playAnimation(walkLeftAnim)
		elseif dotRight > 0.75 then
			stopAnimation(animationControllers["WalkForwardR6"])
			stopAnimation(animationControllers["WalkBackwardR6"])
			stopAnimation(animationControllers["WalkLeftR6"])
			currentAnimationController = playAnimation(walkRightAnim)
		end
		if currentAnimationController then
			currentAnimationController.Priority = Enum.AnimationPriority.Movement
		end
	else
		humanoid.WalkSpeed = 0
		stopAnimation(animationControllers["WalkForward"])
		stopAnimation(animationControllers["WalkBackward"])
		stopAnimation(animationControllers["WalkLeft"])
		stopAnimation(animationControllers["WalkRight"])
	end
end)

Here is the updated version: 8 Way Directional Walking System Open Sourced
Stay tuned for more resources!

9 Likes