Enemy takes no damage

Godot Version

`4.1.2

What the code should do is kill the enemy when its area detects a bullet group node, but even though it doesn’t have any type of error, the enemy doesn’t die. I thought it was because the bullet was destroyed when it touched the enemy’s area, but it didn’t.

enemie code:


@export var movement_speed = 40.0

@onready var planta =  get_tree().get_first_node_in_group("planta")
@onready var sprite = $Sprite2D
@onready var vida = 9


func _physics_process(_delta):
	if is_instance_valid(planta):
		var direction = global_position.direction_to(planta.global_position)
		velocity = direction*movement_speed
		move_and_slide()

		if direction.x > 0:
			sprite.flip_h = false
		
		elif direction.x < 0:
			sprite.flip_h = true
			
	else:
		planta = get_tree().get_first_node_in_group("planta")
		move_and_slide()
	

func _ready():
	$lil.play("move")```








func _on_area_2d_area_entered(_area):
	if _area.is_in_group("planta_area"):
		$lil.play("attack")
	elif _area.is_in_group("bala"):
		vida -= 9
	elif vida == 0:
		queue_free()


func _on_area_2d_area_exited(_area):
	$lil.play("move")

Seems like you are subtracting nine health, and separately checking if their health is zero. I recommend making this an if statement and using less than or equal to zero, since negative numbers would not trigger an equal comparison.

elif _area.is_in_group(“bala”):
    vida -= 9
    if vida <= 0:
        queue_free()

Make sure to format your code correctly, it’s hard to read without formatting.

1 Like

Thank you for your help and also for the code, I didn’t know how to do that.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.