Help with making directional dashing

What do you want to achieve?
I’ve been trying to make a dashing system that uses the key the player is holding(WASD) to determine the direction

What is the issue?
I’m not really sure how to go about it, i’ve used multiple if statements but to me it doesn’t feel like the best practice

What solutions have you tried so far?
This is the current script it only allow the player dash forwards

---- dashing
UIS.InputBegan:Connect(function(input,isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.Q and UIS:IsKeyDown(Enum.KeyCode.W) and dashDeb == false then
		dashDeb = true
		
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {character,workspace.Baseplate}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		
		local rayResult = workspace:Raycast(humRoot.Position,humRoot.CFrame.LookVector * vars.DASH_DISTANCE,raycastParams)
		local rayPos
		if rayResult then
			local part = rayResult.Instance
			if part.CanCollide == true then
				rayPos = rayResult.Position
			else
				rayPos = humRoot.CFrame*CFrame.new(0,0,-vars.DASH_DISTANCE).Position
			end
		else
			rayPos = humRoot.CFrame*CFrame.new(0,0,-vars.DASH_DISTANCE).Position
		end
			
		local mover = Instance.new("BodyPosition",humRoot)
		mover.Name = "Dash"
		mover.MaxForce = Vector3.new(9999999,9999999,9999999)
		mover.D = 100
		mover.P = 1000
		mover.Position = rayPos
		debris:AddItem(mover,0.3)
		
		local gyro = Instance.new("BodyGyro",humRoot)
		gyro.MaxTorque = Vector3.new(9999999,9999999,9999999)
		gyro.D = 100
		gyro.P = 1000
		gyro.CFrame = humRoot.CFrame
		debris:AddItem(gyro,0.3)
		
		spawn(function()
			wait(vars.DASH_CD)
			dashDeb = false
		end)
	end
end)

Any sort of help is really appreciated, Thanks

2 Likes

change these to

humRoot.CFrame * (humRoot.CFrame.LookVector * vars.DASH_DISTANCE)

sorry but i meant for the wasd key, got any ideas for that?

I believe adding a variable like this should work:

local z = UIS:IsKeyDown(Enum.KeyCode.W) and -vars.DASH_DISTANCE or UIS:IsKeyDown(Enum.KeyCode.S) and vars.DASH_DISTANCE or 0
local x = UIS:IsKeyDown(Enum.KeyCode.A) and -vars.DASH_DISTANCE or UIS:IsKeyDown(Enum.KeyCode.D) and vars.DASH_DISTANCE or 0
--may need to switch the negatives for the x variable.

So, it would look something like this:

---- dashing
UIS.InputBegan:Connect(function(input,isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.Q and dashDeb == false then
		local z = UIS:IsKeyDown(Enum.KeyCode.W) and -vars.DASH_DISTANCE or UIS:IsKeyDown(Enum.KeyCode.S) and vars.DASH_DISTANCE or 0
		local x = UIS:IsKeyDown(Enum.KeyCode.A) and -vars.DASH_DISTANCE or UIS:IsKeyDown(Enum.KeyCode.D) and vars.DASH_DISTANCE or 0
		
		if x == 0 and z == 0 then return end
		
		dashDeb = true
		
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {character,workspace.Baseplate}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		
		local rayResult = workspace:Raycast(humRoot.Position,humRoot.CFrame.LookVector * vars.DASH_DISTANCE,raycastParams)
		local rayPos
		if rayResult then
			local part = rayResult.Instance
			if part.CanCollide == true then
				rayPos = rayResult.Position
			else
				rayPos = humRoot.CFrame*CFrame.new(0,0,-vars.DASH_DISTANCE).Position
			end
		else
			rayPos = humRoot.CFrame*CFrame.new(0,0,-vars.DASH_DISTANCE).Position
		end
			
		local mover = Instance.new("BodyPosition",humRoot)
		mover.Name = "Dash"
		mover.MaxForce = Vector3.new(9999999,9999999,9999999)
		mover.D = 100
		mover.P = 1000
		mover.Position = rayPos
		debris:AddItem(mover,0.3)
		
		local gyro = Instance.new("BodyGyro",humRoot)
		gyro.MaxTorque = Vector3.new(9999999,9999999,9999999)
		gyro.D = 100
		gyro.P = 1000
		gyro.CFrame = humRoot.CFrame
		debris:AddItem(gyro,0.3)
		
		spawn(function()
			wait(vars.DASH_CD)
			dashDeb = false
		end)
	end
end)

You can use PlayerModule found inside PlayerScripts to determine the direction of the dash, require it at the top of the script like this:

local Controls = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls();

Then when you want to determine the exact direction you just use Controls:GetMoveVector()
It returns Vector3 with (-1;1) range relative to the camera:
x - left(-1), right(1)
y - always 0
z - forward(-1), backward(1)

With that you can convert it to CFrame like so

rayPos = humRoot.CFrame + (Controls:GetMoveVector() * vars.DASH_DISTANCE);

Thus resulting in an easy implementation without countless IFs from UIS, plus compatible both with keyboard and gamepad/mobile thumbstick if you would like to rework it a bit for those platforms.

2 Likes

Thanks for your help, I had to change it to Humanoid.MoveDirection because of some bugs I encountered with the raycasting

The final script for anyone looking to make a dashing system:

local uis = game:GetService("UserInputService")
local debris = game:GetService("Debris")

local char = script.Parent
local hum = char.Humanoid
local humRoot = char.HumanoidRootPart

local dashDeb = false
local vars = {
	DASH_DISTANCE = 20,
	DASH_CD = 0.5	
}

-- dashing
uis.InputBegan:Connect(function(input,isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.Q and dashDeb == false then
		dashDeb = true
		
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {char,workspace.Baseplate}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

		local rayResult = workspace:Raycast(humRoot.Position,hum.MoveDirection * vars.DASH_DISTANCE,raycastParams)
		local rayPos
		if rayResult then
			local part = rayResult.Instance
			if part.CanCollide == true then
				rayPos = rayResult.Position
			else
				rayPos = (humRoot.CFrame + (hum.MoveDirection * vars.DASH_DISTANCE)).Position
			end
		else
			rayPos = (humRoot.CFrame + (hum.MoveDirection * vars.DASH_DISTANCE)).Position
		end

		local mover = Instance.new("BodyPosition",humRoot)
		mover.MaxForce = Vector3.new(9999999,9999999,9999999)
		mover.D = 100
		mover.P = 1000
		mover.Position = rayPos
		debris:AddItem(mover,0.3)

		local gyro = Instance.new("BodyGyro",humRoot)
		gyro.MaxTorque = Vector3.new(9999999,9999999,9999999)
		gyro.D = 100
		gyro.P = 1000
		gyro.CFrame = humRoot.CFrame
		debris:AddItem(gyro,0.3)

		spawn(function()
			wait(vars.DASH_CD)
			dashDeb = false
		end)
	end
end)
1 Like

How would i make it so if it plays a matching animation depending on how im moving, for example, if im mowing forward i play forward animation, backward i play backward animation etc…