Most efficient way to handle hundreds of "objects in ra

Hi,

So i am supposed to have hundreds of game objects (same prefab) in the terrain and they are supposed to check objects in their sight range (100 units or so) every second or so and react according to what has been found (attack enemy etc.).

So what is the most efficient way to implement this as this seems to be very computing heavy?

Do X numbers of checkups per frame (based on performance) then Yield and continue to check another X on the next frame.
Avoid using the update loop if you don’t need to call a function every frame, use a co-routine.
Cache whatever you can, f.e if you checked the distance between unit A and unit B avoid doing the same check from unit B to unit A.
Cache all getComponent calls,
Always use sqrMagnitude and not Magnitude for your distance checks if it is possible
Do lots of benchmarking, when you are not sure if one command is faster than the other, repeat it a 1000 times and see how long it takes, then check the other command and repeat it a 1000 times, and see which is better.
and don’t forget to Yield…
.

I would drop the freeform distance checking and instead have a virtual grid (quad tree / oct tree alike idea) with a correspondingly choosen side length.
that way you can cut many if not even all distance checks and do checkst against objects in your and adjacent grid cells.

objects can register / unregister themself on cells as they move around

Hmm, this seems efficient but i think there might be problems when the objects are walking near the border of cells (dot is object and | is border):

|. | .| !Object found another

| .| .| !Object found another
(left dot was sitting in same place and right walked to adjanced cell)

or am i missing something?

Maybe a implementation of both grid and freeform might be good. ie make the grid bigger than 100 units and dont make freeform checks if there arent any interesting objects registered to adjanced cells, if there are interesting objects wake up freeform checking to all objects adjanced to interesting object cell (and start behaving when the interesting object is nearer than 100 units or so)…

the grid cells needs to have some overlapping area to avoid problems around the edges.

Ah yeah. Seems pretty clear now. I think i need to make influence map anyway so this virtual grid could be used in that too. thanks both! now i “only” need to figure out how to actually implement this then as im newbie to whole Unity and game programming. :slight_smile:

You could give each of them a “shift” during which they are awake and looking for the target and only process those which are “on shift”. Then when they see the target they stay awake until a certain interval after the target can no longer be seen.

More shifts means less processing load, but worse detection delay.

   if (!wokenUp  timer % numberOfShifts == watchRotation) {
      if (CanSeeTarget()) {
         WakeUp();
      }
   }
   timer++;

This way, the processing load for a single search from each object is spread over numberOfShifts frames.

Or you could have a few “master” objects which have a much longer sight range which wake up nearby objects when the target wanders close enough.

Yes the idea was that they dont search every Update but every second or so.

But my idea was to make patrollers anyway so maybe i will make them search every second and others search every 3 seconds or so which decreases workload nicely as propably 80% of the objects arent suppose to be dealing with enemies usually but only when under attack. (and then i could just make a “broadcast” of the enemy positions to everyone when they have breached some virtual city parameter.)

Have to test te grid too… :slight_smile: