- Home /
The most effective way to search enemies.
Hello.
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 !! :)
Answer by MichalOp · Jan 03, 2015 at 12:49 PM
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.
Every tank will be calling OverlapSphere method in Update method. How does it will affect to performance?
I think it should be okay, in my game I use OverlapSphere for about 50 or more objects and it works well.
Your answer
Follow this Question
Related Questions
move enemy to old position of player 2 Answers
Move 'Player' To new position. 2 Answers
how do i stop the stuttering? 1 Answer
Vector3.RotateTowards Problem 1 Answer