- Home /
Most efficient way to target enemies for a tower defense
I've currently written a bit of code for a td-like game that I'm working on. It's how the tower's find which enemies to target when they are in range of the tower. I'd like to know whether there is a more efficient/effective method to carry out the same job with a different approach.
My approach - GIve the tower a big sphere collider which covers the whole range of the tower. When an enemy triggers the collider, it automatically becomes the target of the tower. If the tower already has a target, it is added to a list of objects that are 'in range' of the tower. When the tower's current target leaves the trigger(OnTriggerExit), it chooses another target in random among the enemies 'in range' of the tower. If there are no enemies 'in range', the tower goes dormant.
I've found that there is also a raycasting approach and the Physics.OverlapSphere approach, which are probably better approaches than my approach when the tower has a target. But considering that both approaches have to scan their vicinity constantly to look for targets(when there are not any), it seems somewhat heavier in my opinion.
The problem is, the td game that I am creating will probably have about a maximum of around 60~70 enemies at the same time, making about 20~30 possible enemies being 'in range' of a single tower. This game will have A LOT of towers(possibly 70~100) in the same screen, meaning a lot of objects being tracked at the same time, which I believe could be more resource-heavy than other approaches.(This is what I am not sure about)
Are there any other more cost-effective methods to target enemies or is my method seemingly efficient enough?
Answer by Harinezumi · Feb 20, 2018 at 08:18 AM
I think your approach of using a trigger SphereCollider and adding those entered to a list is very good. Checking for "collision" with a SphereCollider is relatively cheap in physics (especially if you model your enemies' physics with spheres as well ;) ). The only thing I would change is that in many TD games the player can choose the tower's target selection strategy, usually among "closest to end of path", "random", "least hit points", etc.
My personal opinion is that the raycasting method would definitely be slower, and I think the Physics.OverlapSphere as well, simply because of the constant scanning.
Finally, to suggest an alternative approach, you could completely circumvent physics and just get the list of enemies from a manager for each tower, and check if each enemy is in range (using Vector3.SqrMagnitude, which is a lot faster than Vector3.Magnitude). This would have about an O(enemies * towers) cost, but not using physics at all.
Exactly the answer I was looking for. I really like your alternative approach as well, which I will test out in the future if my approach turns out to be laggy in certain situations.(I'll give you a heads-up on the post once I have done) Thanks a lot for the reply.
Your answer
![](https://koobas.hobune.stream/wayback/20220612153014im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
getting enemys to follow character. 1 Answer
How to use collision to enable light? 1 Answer
help please :S? im confusing myself. 2 Answers
Turn on lights based on proximity to controller. 1 Answer
Enemy Proximity Script 0 Answers