How to do dash in a direction even when not moving

Godot Version

Godot 4.2.2.stable

Question

So basically I can dash when I am moving (this is due to getting the axis while moving and multiplying it by the dash speed)
However when I stop still, because I’m not getting my direction I cannot dash. Is there any way to implement this?

Here is my code:

var direction = Input.get_axis("left", "right")
	
	#movement
	
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

#dashing

	if Input.is_action_just_pressed("dash") and can_dash:
		is_animation_playing = true
		$AnimatedSprite2D.play("dash_roll")
		dashing = true
		velocity.x = direction * DASH_VELOCITY
		$Timer.start()
		can_dash = false

In your direction, your are getting your input. So the result is 0 or 1, or -1.
Later, in your dash input, your code is

dashing = true
velocity.x = direction * DASH_VELOCITY

Since direction is equal 0, your velocity.x will be 0. You have to code different, where your velocity.x receive only the DASH_VELOCITY. You can’t just delete in this code of yours, you will need to adjust.

@sSmothie ,in the part of the code where you walk in the direction, you can unload the current direction in an last moved direction variable, so it would use the last moved direction in the dash.

var direction = Input.get_axis("left", "right")
var last_moved_dir=0
	
	#movement
	
	if direction:
                last_moved_dir=direction
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

#dashing

	if Input.is_action_just_pressed("dash") and can_dash:
		is_animation_playing = true
		$AnimatedSprite2D.play("dash_roll")
		dashing = true
		velocity.x = last_moved_dir* DASH_VELOCITY
		$Timer.start()
		can_dash = false
1 Like