Multidirectional Dash System

I want to make a multidirectional dash system (the animation changes depending on what key the player is pressing like W, A, S or D) but don’t know how to.
I’ve made a script but it only works for forward dashing and I don’t know how to make it so it goes multiple directions.
This is the script so far:

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local DashAnimation = script:WaitForChild('Animation')

-- Settings
local key = Enum.KeyCode.Q -- key to trigger dash
local velocity = 11500 -- dash speed
local debounce = false 
local cooldown = 2 -- cooldown after dash
local duration = 0.3 -- dash duration
--

local function Dash()
	local character = player.Character
	if character and not debounce then
		debounce = true

		local humanoid = character.Humanoid
		local HRP = character.HumanoidRootPart -- HRP stands for HumanoidRootPart

		local loadedAnimation = humanoid.Animator:LoadAnimation(DashAnimation)

		local dashDirection = nil
		local moveDirection = humanoid.MoveDirection
		local lookVector = HRP.CFrame.LookVector
		local minusVelocity = -velocity -- in CFrame, if we set Z to positive, player will go move backward
		-- instead of forward

		-- checking if player is on ground, not floating
		local isOnGround = humanoid.FloorMaterial ~= Enum.Material.Air and humanoid.FloorMaterial ~= Enum.Material.Water

		if isOnGround then


			if moveDirection == Vector3.new(0,0,0) then -- if player is not currently moving/walking
				dashDirection = HRP.Position + Vector3.new(lookVector.X, 0, lookVector.Z)
			else -- if player are currently moving/walking
				dashDirection = HRP.Position + Vector3.new(moveDirection.X, 0, moveDirection.Z)
			end

			-- using bodygyro to rotate player to the dash direction smoothly
			local bodyGyro = Instance.new("BodyGyro")
			bodyGyro.Parent = HRP
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
			bodyGyro.D = 0 -- D is the dampening
			bodyGyro.P = 500000 -- P is aggressiveness
			bodyGyro.CFrame = CFrame.lookAt(HRP.Position, dashDirection) -- Making player look at the dash direction

			local attachment = Instance.new("Attachment")
			attachment.Parent = HRP

			-- now using VectorForce to move player forward
			local vectorForce = Instance.new("VectorForce")
			vectorForce.Parent = HRP
			-- VectorForce need attachment to tell where is player looking at
			vectorForce.Attachment0 = attachment
			vectorForce.Force = Vector3.new(0,0,minusVelocity) -- now it will move player forward as the settings

			loadedAnimation:Play() -- play the dash animation
			humanoid.AutoRotate = false -- prevent player to rotate by themselves

			wait(duration)

			humanoid.AutoRotate = true

			vectorForce:Destroy()
			bodyGyro:Destroy()
			attachment:Destroy()

			--wait(duration) -- give some time before stopping the dash animation
			loadedAnimation:Stop()

		end


		--
		wait(cooldown)
		debounce = false
	end
end

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == key then
		Dash()
	end
end)

detect the last input and if its wasd then dash in that respective order or you can detect if the player is holding that key and then dashes

edit: oh you meant as in animation?

You can try this!

local UserInputService = game:GetService(“UserInputService”)

local shiftDown = false
local direction = “”

local function onInputBegan(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
shiftDown = true
elseif input.KeyCode == Enum.KeyCode.W then
direction = “forward”
elseif input.KeyCode == Enum.KeyCode.A then
direction = “left”
elseif input.KeyCode == Enum.KeyCode.S then
direction = “backward”
elseif input.KeyCode == Enum.KeyCode.D then
direction = “right”
end
end

local function onInputEnded(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
shiftDown = false
elseif input.KeyCode == Enum.KeyCode.W and direction == “forward” then
direction = “”
elseif input.KeyCode == Enum.KeyCode.A and direction == “left” then
direction = “”
elseif input.KeyCode == Enum.KeyCode.S and direction == “backward” then
direction = “”
elseif input.KeyCode == Enum.KeyCode.D and direction == “right” then
direction = “”
end
end

local function onStep()
if shiftDown and direction ~= “” then
– load and play the appropriate animation
– based on the direction variable
end
end

UserInputService.InputBegan:Connect(onInputBegan)
UserInputService.InputEnded:Connect(onInputEnded)
game:GetService(“RunService”).Stepped:Connect(onStep)

2 Likes

Yeah, like for every direction a different animation.

thats simple, just make 4 different animation tracks one for each direction, then play them for the respective direction

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.