Dashing in 2d game, glitching

Godot 4

I’m making a new game and im experiencing a glitch when my character tries to dash. It happens when switching between moving left and right quickly and then dashing instead of dashing the full length the dash gets cut short. This also happens (sometimes) when dashing and hitting an enemy. The next dash will be cut short

Any and all help is greatly appreciated.

Heres the code:

class_name Dashstate
extends Player_State
#-------------------------------------------
@export var fall_state : Player_State
@export var ground_state : Player_State
@export var death_state : Player_State
@export var hurt_state : Player_State
@export var dash_animation : String = "Dash"
@export var run_animation_node : String = "Run"
@export var fall_animation_node : String = "Fall"
#-------------------------------------------
@onready var timer = $Dash_Timer
#-------------------------------------------
const dash_speed = 900
#-------------------------------------------
var dashing : bool = false
#-------------------------------------------

func on_enter():
	timer.start()
	dashing = true

#dash effect logic
func state_process(delta):
	if (dashing == true):
		$"../../CPUParticles2D".emitting = true
		$"../../CPUParticles2D2".emitting = true
	elif (dashing == false):
		$"../../CPUParticles2D".emitting = false
		$"../../CPUParticles2D2".emitting = false
#--------------------------------------------
#stop dash and get knockbacked
	if (Game.hurt == true and not Game.playerHP <= 0):
		dashing = false
		next_state = hurt_state
#--------------------------------------------
#stop dash if dead 
	if (Game.playerHP <= 0):
		dashing = false
		next_state = death_state
#--------------------------------------------
#The actual dash logic
	var direction = Input.get_vector("ui_left", "ui_right","ui_up","ui_down")
	if (direction.x == 0):
		character.velocity.x = character.latest_direction * dash_speed
	else:
		character.SPEED = dash_speed

#dash end and switch to relevant state
func _on_timer_timeout():
	$"../../CPUParticles2D".emitting = false
	$"../../CPUParticles2D2".emitting = false
	character.SPEED = 300
	if (character.is_on_floor()):
		playback.travel(run_animation_node)
		next_state = ground_state
	elif(character.is_on_floor() == false):
		dashing = false
		playback.travel(fall_animation_node)
		next_state = fall_state


this could just be you block/ignore player input when dashing is active. so it wont interrupt the dashing period

How would you go about coding that?

(new to coding)

change to:

var direction = Input.get_vector("ui_left", "ui_right","ui_up","ui_down")
	if (direction.x == 0) and not dashing:
		character.velocity.x = character.latest_direction * dash_speed
	else:
		character.SPEED = dash_speed

I think you’re onto something (the issue is in the two dashing scripts) but with the change the character can’t dash when standing still.

Edited:
The glitch still happens even when removing the other dash logic

i think your dashing boolean is not properly reset to false value when timer timeout

is it true the dashing is set to false only after the timer timeout when the player not on the floor?

well yeah, but i dont think that is what’s causing the issue.
Forgot to write in the main post but the glitch also happens (sometimes) when dashing into an enemy

have you tried also changing this?:

#dash end and switch to relevant state
func _on_timer_timeout():
	$"../../CPUParticles2D".emitting = false
	$"../../CPUParticles2D2".emitting = false
	character.SPEED = 300
	if (character.is_on_floor()):
		playback.travel(run_animation_node)
		next_state = ground_state
	elif(character.is_on_floor() == false):
		playback.travel(fall_animation_node)
		next_state = fall_state
	dashing = false

i just wanted to know if the player can dash now when standing still

yeah, but the dashing variable is primarily used for the dash effect

if (dashing == true):
		$"../../CPUParticles2D".emitting = true
		$"../../CPUParticles2D2".emitting = true
	elif (dashing == false):
		$"../../CPUParticles2D".emitting = false
		$"../../CPUParticles2D2".emitting = false

Not so much for the dash itself

The state_process(delta) calls the dash every frame so the velocity is faster on every frame until the timer ends. Then the speed is changed back and the states are switched