How can I make Megaman X's dash?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DaviBraid

I got really, really close to what I wanted.
There’s only 1 issue with this code (besides the fact (it is a mess),
if the player hits the ground while holding the Dash Button, the character will dash and that’s just not good.

func _physics_process(delta):
var move_dir = 0
var dash = 1
var grounded = is_on_floor()
Normal Run
if Input.is_action_pressed("ui_right"):
	move_dir += 1
if Input.is_action_pressed("ui_left"):
	move_dir -= 1
Dash movement
if grounded and Input.is_action_pressed("Dash"):
	print (timedash)
	timedash += delta
if is_on_wall():
	timedash = 0
	dash_impulse = 1
if timedash == 0 or timedash > 0.6:
	dash = 1
if timedash > 0 and timedash < 0.6 and grounded:
	dash = 2.333333333333333
if Input.is_action_just_released("Dash"):
	timedash = 0
if grounded:
	dash_impulse = 1
if not grounded and timedash > 0:
	dash_impulse = 2.333333333333333
GENERAL SPEED CODE
move_and_slide(Vector2(move_dir * MOVE_SPEED * dash * dash_impulse, y_velocity), Vector2(0,-1))

It’s not clear what you are trying to achieve.

It seems like you are using 2 variable (dash and dash_impulse) which are basically the same, and multiply them together to calculate the final value of the dash speed.
it’s a little twisted (i mean, you could have done the same with a single variable), but it could work. However your code is quite a mess of if, you are probabling just confusing yourself on what values to assign in each situation.

Also, the issue could come from the the is_on_floor() and is_on_wall() function, as it updates only after move_and_slide() is called. Try printing grounded on each frame and check if the results are as you expect.

Andrea | 2021-01-15 13:36

The goal: to make the character dash if they press and hold the dash button while on the ground

the issue: if the character is holding the dash button while jumping, he’ll dash as soon as he lands

Note:
the dash_impulse is to maintain momentum when jumping during a dash. The momentum only ends when the character lands, as intended.

DaviBraid | 2021-01-15 14:20

well, of course it will dash as soon as it touch land, this is what you told it to do with

func _process(delta):
    if grounded and Input.is_action_pressed("Dash"):
        timedash += delta

since this piece of code is inside the _process() function, it will check for this condition on everyframe: if the player start holding dash while on air, when the character touch the ground, the condition became true and the dashing begins.

if you want the character to start dashing only if the player press Dash when on ground, you should use the func _input(event), that fire only when an input is received (or change the way your code execute the dashing alltogether)

Andrea | 2021-01-15 14:58

:bust_in_silhouette: Reply From: Juca_Valo

Hi, Davi! Could you share your complete code? I’m trying for 3 days to make a dash+jump, but I just can’t find any tutorial, much less achieve it alone. Please help!

Look at my answer may it will help you

kareem_farrag | 2022-12-28 16:23

:bust_in_silhouette: Reply From: kareem_farrag

Make a timer for dash by adding a timer and repeat the same code but edit it
func _process(delta):
if grounded and Input.is_action_pressed(“Dash”):
timedash += delta

func _process(delta):
if !is_on_floor and Input.is_action_pressed(“Dash”):
gravity = 0
timer.start()
timedash += delta