0
\$\begingroup\$

Situation:

  • I have a grid of 100 by 100 tiles.
  • each tile has a list of units that are currently standing ontop of it.
  • in this grid 500 units of team 1 and 500 units of team 2 are randomly walking around.
  • while they are walking around they place themselves in the unitlist of each tile they are on.

problem: Each unit has to check lets say a square of 6 by 6 tiles around the tile he is on, to check if there is an enemy nearby.

my solution. each unit checks on top of what tile he is and only when it changes tiles it will check the 36 tiles around its current tile and check all their unitlist to check for nearby enemies...

Question.

  1. isn't there a better way to do this? Because when I need my units to have a bigger line of sight of lets say 15tiles it needs to check 1000units * 15tiles *15tiles = a gazillion...
  2. Does there exist a very inexpensive way of using somekind of a basic circle collider in unity without the need for rigid bodies etc...?
\$\endgroup\$
  • \$\begingroup\$ Silly question: Does it matter whether a tile has 1 or 1000 enemy units? Because if not, then you can create a model of the map that contains a list of players in each tile and then check against that. So, no matter how many units of player 1 are in a tile, it's still only player 1 in the new map model. \$\endgroup\$ – pek Mar 27 '14 at 0:59
1
\$\begingroup\$

Yes, Unity contains physics OverlapSphere function. This requires the objects have colliders, but does not require rigid bodies. The performance of this is hard to judge until you actually use it in your implementation.

If you run into performance issues, I would suggest however you implement some kind of quad/octree as suggested in this related question. Additionally, there are a number of algorithms defined for nearest neighbor searches, you should read through them and see what fits the requirements of your project.

\$\endgroup\$
  • 1
    \$\begingroup\$ I can understand that performance can only be checked in the actual implementation but as rule of thumb... Is using the physics.overlapSphere a 1000 times (for a radius of 10) faster or slower then looping through 100 c#objects a 1000 times? \$\endgroup\$ – Ruben O Mar 26 '14 at 12:06
  • \$\begingroup\$ It's faster. If you have an implementation to test this stuff out you should give it a try. \$\endgroup\$ – MichaelHouse Mar 26 '14 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.