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