Multi directional dodges in 2d space (Top View)

I want to achieve multi-directional dodges, I’m making top view story-driven / co-op shooter and I’m trying to make that when a player is holding S then presses Space bar it initiates dash in that direction. the problem is i can’t do it sideways, so started working on a “blocker” when multiple buttons are pressed as an example lets say a player is holding down S and D it disables dashes S and D then it activates the ability to do dash sideways.

Some example

[Variables here]

Instance.new("BodyForce",torso)	
torso.BodyForce.Name = "Dodge"
torso.Dodge.Force = Vector3.new(0,0,0)	 
debounce = false

function DodgeDown()
	torso.Dodge.Force = Vector3.new(-14000,1,0)
	 humanoid.WalkSpeed = 0	    
  wait(0.25)
	torso.Dodge.Force = Vector3.new(0,0,0)
  wait(0.25)
	 humanoid.WalkSpeed = 16
end	

local function Down()
	return UIS:IsKeyDown(S) and UIS:IsKeyDown(Space)
end

local function Input(input, gameProcessedEvent)
	if debounce then return end 
	if Down() then	debounce = true		
print("Going Down")
		   DodgeDown()
		debounce = false
	end
end


— updated the code to the latest reliable build

3 Likes

Couldn’t you go in the direction of the lookVector of the humanoid root part? if you implement this you wouldn’t need a function for every direction.

Nope, player character constantly follows mouse cursor.
Just forgot to turn it on.
here


i will later animate it.

You could use a part that doesn’t rotate with the character (has a fixed orientation) and then use lookVector, rightVector on that part to get a cframe for the root to go to, or you could create a cframe with only the position updating.

ok,
but the problem of dashes side ways would sitll persist
i need help with those darn imputs

You could use userInput:IsKeyDown() to detect which keys are down, and then have a dictionary with the directions of them like this (I’m sorry if there are any issues with this cause I didn’t write this in studio):

local keyDir = {
 Enum.KeyCode.S = CFrame.new(0,0,10), --Goes ten studs backwards
 Enum.KeyCode.D = CFrame.new(10,0,0),
 ...
}

And then just multiply them like this:

local cframeToGoTo = char.HumanoidRootPart.CFrame

for _,v in pairs(userInput:GetKeysPressed()) do --keysDown would be a table with the keys that are currently down, but check if they are one of WASD.
 if keyDir[v] then
  cframeToGoTo * keyDir[v]
 end
end

--Tween to the cframeToGoTo.
1 Like

Sounds and looks like a good plan,
I’m gonna try it out straight away

I’ve changes the for loop a bit as it seems there is a function which gets an array of keys currently pressed. (userInput:GetKeysPressed())

Another edit: Replaced the v.KeyCode with simply v as it already returns it as a KeyCode enum. Be careful: change the if statements v.KeyCode aswell!

2 Likes

I can’t make it work at all for some reason, it may be my incompetence.
I’m still new to this stuff.

Maybe i should stick to my old build,
it seems much easier to run and animate

Hey! I’m sorry for seeing this this late but have you looked at the adjustments I made, and do you get any errors?

yes I do and I was trying to figure out a way to fix them :

local keyDir = {
Enum.KeyCode.S = CFrame.new(0,0,10),
Enum.KeyCode.D = CFrame.new(10,0,0), }

Expected a name, got a complex expression.

and

char = game.Players.LocalPlayer.Character

local cframeToGoTo = char.HumanoidRootPart.CFrame

for _,v in pairs(userInput:GetKeysPressed()) do
if keyDir[v] then cframeToGoTo * keyDir[v]
end
end

Incomplete statement expected assignment or a function call.

Once again sorry for the late response as I went to sleep.
But this should do it:

New code:

for _,v in pairs(userInput:GetKeysPressed()) do
local keyCFrame = keyDir[userInput:GetStringForKeyCode(v)

if keyCFrame then cframeToGoTo * keyCFrame
end
end

Replace the dictionary with:

local keyDir = {
 ["S"] = CFrame.new(0,0,10),
 ["D"] = CFrame.new(-10,0,0),
 ["W"] = CFrame.new(0,0,-10),
 ["A"] = CFrame.new(10,0,0),
}
1 Like

No worries no one here is in a hurry.

cframeToGoTo is still an Incomplete statement, expected assignment or a function call.

looking for solution

update

so far I found out that’s not the cframeToGoTo issue but this spot in general

for _,v in pairs(userInput:GetKeysPressed()) do
local keyCFrame = keyDir[userInput:GetStringForKeyCode(v)]
if keyCFrame then [keyCFrame] * cframeToGoTo

same error though

Hey! I had written a fix for that issue but I redid the big post from earlier, so I forgot about the issue and rewrote the code with the error in yet again.

Replace the if statement with this:
if keyCFrame then cframeToGoTo = cframeToGoTo * keyCFrame end

This was a dumb error on my side :smiley:.

1 Like

now its says that userInput means nothing in this script.
“Unknow global userimput”

  1. Make sure you spelled userInput with a capital i instead of userimput.
  2. Make sure you have local userInput = game:GetService("UserInputService") at the top of your script.
1 Like

though i missed something …
it may sound dumb on my side but how do I activate it ?
from my understanding, it should drag my character while pressing asdw right?

How do you want it to trigger?

By pressing space bar while holding one or two movment buttons