Cant dash while moving

Godot Version

4.7

Question

I made a script that lets me dash while standing still, but the second i start moving, i cant press it anymore. also, i can spam the dash button to bypass cooldowns??? im so confused is there some sort of ordering i dont understand yet? please help im quite new to godot

extends CharacterBody2D


const SPEED = 130
const JUMP_VELOCITY = -300

# Dash variables
const DASH_SPEED = 800
const DASH_LENGTH = .1
@onready var last_dir = 1

# Setup dash timer
@onready var dash_timer = $"Dash Timer"

func start_dash(dur):
	dash_timer.wait_time = dur
	dash_timer.start()
	
func is_dashing():
	return !dash_timer.is_stopped()


@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	
	# Get the input direction: -1, 0, 1
	var direction := Input.get_axis("move_left", "move_right")
	
	# Dashing logic
	if not direction == 0:
		last_dir = direction
	
	if Input.is_action_just_pressed("dash"):
		start_dash(DASH_LENGTH)
	if is_dashing():
		velocity.x = DASH_SPEED * last_dir
	
	
	# Flip the Sprite
	if direction > 0:
		animated_sprite.flip_h = false
	elif direction < 0:
		animated_sprite.flip_h = true
	
	# Play animations
	if is_on_floor():
		if direction == 0:
			animated_sprite.play("idle")
		else:
			animated_sprite.play("run")
	else:
		animated_sprite.play("jump")
	
	
	
	
	# Apply movement
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

Your movement code is overriding your dash code.

if is_dashing():
		velocity.x = DASH_SPEED * last_dir

#later in script

if direction:
		velocity.x = direction * SPEED

Since the basic movement code comes after the dash code, velocity.x will always equal direction * SPEED. This should be fixed if you use if direction and not is_dashing() for your movement code instead.

In full:

# Apply movement
	if direction and not is_dashing():
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

And to prevent you from bypassing the dash cooldown, you need to do something like this, it seems.

if Input.is_action_just_pressed("dash") and not is_dashing():
	start_dash(DASH_LENGTH)

thank you so much i havent been able to figure that out

doesnt work ¯\_(ツ)_/¯

Could it be that your DASH_LENGTH is too small? If you increase it to, say, 0.5, it becomes clearly apparent that you cannot use the dash again while you are already dashing.

nope, and increasing dash length to 0.5 makes it dash insanely far as dash length multiplies dash speed

small note:
the timer is controlled using a timer node

figured it out, i forgot the add the timer script :expressionless:
sorry for wasting ur time