The most effective way to search enemies.


I’m writing simple arena like game. Inside arena there will be my tanks and enemy tanks. Tanks has part that moves whole body and turret part that is child of moving body. Tanks will move randomly. Turrets also got attack range. What is the most effective way to detect and attack enemies that will not affect for performance of game?

I have one idea.

Every turret will store all GameObject references of all enemies. There will be function that will be called inside Update that will detect enemy that is closest and inside attack range. However, I think calling at update searching function is not effectvie.

Also, Happy New Year !! :slight_smile:

You can use Physics.OverlapSphere method, which returns a list of all colliders inside a sphere. Then check for every collider if it is an enemy, store the closest and shoot him! You can also make a trigger collider around every turret, and search for enemies in OnTriggerStay or OnTriggerEnter instead in Update.