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