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?