I try to make a dash for my character and it's seem to work fine but if I hold left or right and press dash button the character only dash a little then continue to move normally 🙁
I think this happened because I switch dash state to move state before the character finished the dash. Or maybe it just something else.
Here is my code:

enum{
	IDLE,
	MOVING,
	SHOOTING,
	GUN_N_RUN
	DASH,
	DIE
}

const JUMP = -1000
const GRAVITY = 30
const BULLET = preload("res://BULLET/BULLET 1.tscn")
var SPEED = 600
var states = MOVING
var velocity = Vector2.ZERO
var can_attack = true
var can_dash = true

func _physics_process(_delta):
	print(velocity.x)
	match states:
		IDLE:
			pass
		MOVING:
			movement()
			if Input.is_action_pressed("ui_attack"): 
				states = SHOOTING
			if Input.is_action_just_pressed("ui_dash"):
				states = DASH
		SHOOTING:
			shoot()
			velocity.x = 0
			if Input.is_action_just_released("ui_attack"):
				states = MOVING
			if Input.is_action_pressed("ui_right") or Input.is_action_pressed("ui_left"):
				states = GUN_N_RUN
			if Input.is_action_just_pressed("ui_dash"):
				states = DASH
		GUN_N_RUN:
			shoot()
			movement()
			if Input.is_action_just_released("ui_attack"):
				states = MOVING
			if Input.is_action_just_pressed("ui_dash"):
				states = DASH
		DASH:
			dash()
			if can_dash != true:
				states = MOVING

		DIE:
			pass

func movement():
	if Input.is_action_pressed("ui_right"):
		velocity.x = SPEED 
		$Sprite.flip_h = false 
	if Input.is_action_pressed("ui_left"):
		velocity.x = -SPEED 
		$Sprite.flip_h = true
	velocity.x = lerp(velocity.x, 0, 0.1)
	fall()

func shoot():
	if can_attack:
		var direction = 1 if not $Sprite.flip_h else -1
		var bullet = BULLET.instance()
		bullet.direction = direction
		get_parent().add_child(bullet)
		bullet.position.x = position.x + 200 * direction
		bullet.position.y = position.y 
		$Reload.start()
		can_attack = false

func dash():
	if can_dash:
		var direction = 1 if not $Sprite.flip_h else -1
		velocity.x = SPEED * 6 * direction
		can_dash = false
		$"Dash CD".start()
		$AnimationPlayer.play("Dash")
	fall()

func jump():
	if Input.is_action_just_pressed("ui_up"): #and is_on_floor():
		velocity.y = JUMP

func fall():
	velocity.y = velocity.y + GRAVITY 
	velocity = move_and_slide(velocity, Vector2.UP)

func _on_Hurt_Box_area_entered(_area):
	stats.health -= 1

func _on_Stats_no_health():
	queue_free()

func _on_Reload_timeout():
	can_attack = true

func _on_Hurt_Box_body_entered(_body):
	stats.health -= 1


func _on_Dash_CD_timeout():
	can_dash = true

Just looking at it, it looks like you want action_pressed for dash, not just_pressed. Unless it's like a switch that you toggle or something. But you do have a timeout for it, so I'm not sure what you are trying to do there.

  • ueih replied to this.

    ueih You'll have to work on your logic. It's too hard to read especially because the code tags aren't working. Find out if it's staying in dash state or dropping out. If it's still in dash state, then it's an animation problem. If it's dropping out, why?

    • ueih replied to this.

      fire7side Got it, I just realized my condition to switch from dash to moving make no sense. So I connect the animation finished signal to get the character out of the dash state. Now it work great
      Thanks : 3