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()