4.1.2
I am having two errors with the enemy that I do not understand, first that when the enemy reaches the player he dies but only if I shoot him instead of when I shoot him and his life runs out he dies without having to reach me and then the enemy starts again white color of the hit flash animation although I didn’t put any of that in the code
Sorry if some things are in Spanish, vida is life.
@export var movement_speed = 55
@onready var player = get_tree().get_first_node_in_group("player")
@onready var sprite = $Sprite2D
@onready var vida = 1
@onready var flash = $hitflash
func _physics_process(_delta):
if is_instance_valid(player):
var direction = global_position.direction_to(player.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
func _ready():
$anim.play("idle")
func _on_area_2d_area_entered(_area):
if _area.is_in_group("bala"):
flash.play("flash")
vida -= 9
elif vida <= 0:
set_physics_process(false)
$anim.play("death")
await get_tree().create_timer(2).timeout
queue_free()````
The enemy was taking a long time to die so I gave him 1 life and I don’t know why the enemy had to reach the player before dying and I don’t know why the enemy starts blank.
Can you explain to me why this is wrong? because I don’t understand very well, I mean I copied the code from another enemy that was chasing another node but I changed the node I was chasing to the player node, by the way, bala is bullet
if bala else
else mean is not bala.
then mean you is everything but not your bullet
you needed use if inside if
func _on_area_2d_area_entered(_area):
if _area.is_in_group("bala"):
flash.play("flash")
vida -= 1
if vida <= 0:
set_physics_process(false)
$anim.play("death")
await get_tree().create_timer(2).timeout
queue_free()