How to make enemies only disappear when attacked

Godot Version

Godot 4.4.1

Question

Hello, I am currently following a tutorial on how to make enemies react to an attack.

So far, everything was going well, until the final part. Here, the enemy just vanishes the moment the player character gets close, without using the weapon.

Here is the code for the player:


var screen_size

var lastAnimDirection: String = "down"
var ishurt: bool = false
var isAttacking: bool = false


@onready var animations = $AnimationPlayer

@export var speed: int = 50
@export var maxhealth = 3
@export var knockbackPower: int = 500
@export var inventory: Inventory

@onready var currentHealth: int = maxhealth
@onready var effects = $Effects
@onready var hurttimer = $hurttimer
@onready var hurtColor = $Sprite2D/ColorRect
@onready var hurtbox = $hurtbox
@onready var weapon = $weapon

@onready var sprite = $Sprite2D
@onready var heartsContainer = $CanvasLayer/heartscontainer


func _ready():
	effects.play("RESET")


func handleInput():
	var moveDirection = Input.get_vector("ui_left","ui_right", "ui_up", "ui_down")
	velocity = moveDirection*speed

	if Input.is_action_just_pressed("attack"):
		attack()


func attack():
	animations.play("attack" + lastAnimDirection)
	isAttacking = true
	weapon.visible = true
	await animations.animation_finished
	weapon.visible = false
	isAttacking = false


func updateAnimation():
	if isAttacking: return
	
	if velocity.length() == 0:
		if animations.is_playing():
			animations.stop()
	else:
		var direction = "down"
		if velocity.x < 0: direction = "left"
		elif velocity.x > 0: direction = "right"
		elif velocity.y < 0: direction = "up"
	
		animations.play("walk" + direction)
		lastAnimDirection = direction


func _physics_process(delta):
	handleInput()
	move_and_slide()
	updateAnimation()


	if !ishurt:
		for area in hurtbox.get_overlapping_areas():
			if area.name == "hitbox":
				HurtByEnemy(area)
	
	

func HurtByEnemy(area):
	currentHealth -= 1 
	if currentHealth < 0:
		currentHealth = maxhealth
			
	healthChanged.emit(currentHealth)
	ishurt = true
		
	knockback(area.get_parent().velocity)
	effects.play("Blink")
	hurttimer.start()
	await hurttimer.timeout
	effects.play("RESET")
	ishurt = false


func _on_hurtbox_area_entered(area: Area2D):
	if area.has_method("collect"):
		area.collect(inventory)

func knockback(enemyVelocity):
	var knockbackDirection = (enemyVelocity - velocity).normalized() * knockbackPower
	velocity = knockbackDirection
	print_debug(velocity)
	print_debug(position)
	move_and_slide()
	print_debug(position)
	print_debug(" ")
	


func _on_hurtbox_area_exited(area: Area2D): pass

An here’s the code for the enemy:

extends CharacterBody2D

@export var speed = 20
@export var limit = 0.5
@export var endPoint: Marker2D

@onready var animations = $AnimatedSprite2D
@onready var player = $player

var startPosition
var endPosition

func _ready():
	startPosition = global_position
	endPosition = startPosition + Vector2(10, 3*16)

func changeDirection():
	var tempEnd = endPosition
	endPosition = startPosition
	startPosition = tempEnd

func updateVelocity():
	var moveDirection = (endPosition - global_position)
	if moveDirection.length() < limit:
		changeDirection()
		
	velocity = moveDirection.normalized()*speed

func updateAnimation():
	var animationString = "WalkUp"
	if velocity.y > 0:
		animationString = "WalkDown"
	animations.play(animationString)

func handleCollision():
	for i in get_slide_collision_count():
		var collision = get_slide_collision(i)
		var collider = collision.get_collider()
		print_debug(collider.name)
		

func _physics_process(delta):
	updateVelocity()
	move_and_slide()
	updateAnimation()


func _on_hurtbox_area_entered(area):
	if area == $hitbox: return
	queue_free()
	

What could be done?

Taking quick look at the code, are you sure this signal only fires when the player is attacking? It could be that the player triggers it whenever the hitbox it owns gets close enough to overlap.