How do I make an ai that can target one enemy at a time while having multiple in its range?

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

Im making a game similar to Clash Royale but I cant figure out a way to have an ai I spawn target an enemy ai and switch to another target enemy after the first one is dead. The best way I can think of doing this would be to have a bunch of raycasts shooting in all directions and whenever it collides with an enemy, it targets that one.

:bust_in_silhouette: Reply From: CharlesMerriam

I’m not quite sure what you are asking. It sounds like:

Each AI Spawn will have one enemy, but sometimes you need to switch the enemy to one within some range. I think you are thinking of having an Area2D with a child CollisionShape2D (circle). You can then call the Area2D.get_overlapping_bodies() to find the closest new enemy within range. Alternately, you could just walk through the entire list of enemies and find the closest one.

Hope this helps. Keep hacking. Keep notes.

YES! Thats Exactly what im asking. You solved my problem thanks, but how does the
get_overlapping_bodies() function work? Does it put the closest object in range at the first spot in the array, or is it random?

Dumemes | 2022-05-17 19:30