How to create a proper enemy detection?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By da_dev

So, I have a KinematicBody2D with Area2D and I use it to detect enemies who enter the detection radius of the body. Here’s the code for the signal:

func _on_Detection_area_body_entered(body):
    enemy = body

Problem is that if there’s another body behind the first one it’ll react to it and enemy will be equal to the new body. I still want to detect the first enemy, kill it and only then switch to the second one. I tried many other ways (this is the first one) and none of them work.
Any idea how to do it?

:bust_in_silhouette: Reply From: rustyStriker

Well, you can use another variable to use as a queue for the enemies that come after the first one, that way you always get the first enemy selected and all the other enemies registered in an organized way

I’ve just tried that, but when I kill the first enemy soldier just doesn’t react because area2d doesn’t update.

P.S. Just fixed that by modifying thearea_body_exited. Thank you!

da_dev | 2019-06-26 19:46