HI!I have script, which works so good without FindClosestEnemies(); function. Here is part of code:
With this function, i have <15fps, without >200; What Should I do? I think i shoud change this: gos = GameObject.FindGameObjectsWithTag("Player");, but i have not any ideas how to do it. (I make RTS game^ and this cannon's(Turrel) script(part of one)
[Edit by Berenger, code formatting]
The best way depends on how many enemies / objects will be there, what's the max-distance and how are they distributed across the map.
I know in a lot questions people suggest to use FindGameObjectsWithTag, but keep in mind it has to iterate through all objects so it's quite slow. OverlapSphere uses the physics system which probably uses some kind of space partitioning which minimize the overhead.
Your sorting loop is already in it's optimal form ;)
answered Apr 29 '12 at 11:47 AM
It certainly better to get the reference to things like player once at the start. Whether its going to make a lot of difference to your routine I don't know.
So do something like:
Thats good practice regardless.
answered Apr 28 '12 at 10:17 PM
Call it once every second or two. It will even make the enemies look cooler (when a better target comes closer, they'll sometimes take a few shots at the old target before noticing.)
"Easy" way is a counter:
Slightly CPU faster way is a coroutine, called at start, like:
answered Apr 29 '12 at 08:35 AM