Fast Distance Checking for many objects? (RTS Game)
Hello,
so I'm making a RTS Game in Unity. The units have multiple variables assigned to them, one of them is awareRange (float), which is the radius which is checked if an enemy is inside or not. If an enemy is inside, the Unit attacks it, if multiple enemies are inside, it attacks the first one that entered it.
My problem is the performance of such a check. The game could have more than 1000 units, all calling the same code in Update.
Is there any resource-friendly solution for this?
Thanks in advance
Computationally, distance in 3-space requires a square root for the exact value in most cases (excluding unitary and zero), which can be problematic when you require large numbers of these checks.
To optimize this sort of problem, it can help to start thinking about what does and doesn't need to happen at all times. Does an object 100 units away need to check constantly whether something that can only move 5 units/s entered a 10 unit radius?
For starters, have you tried checking the squared distances ins$$anonymous$$d? Computers are much better at multiplication than they are with taking roots.