Objects keeping tabs on other objects without killing performance

Hi all,

I am trying to figure out the best way to have upwards of 100 objects keeping track of each other and the distance between them without taking a huge hit to performance. My project is a Space Sim type PC build where a solar system can have a bunch of ships traveling between waypoints, spawning, etc. These ships represent about 5 different factions that may or may not be in war with each other (event based faction changes).

Currently for the main player, each ship listens for the Player tag and when its within a certain distance it calls a function to check if it needs to be hostile or not towards the Player but now want the NPC ships to do the same with other NPC ships that are either spawning into the system or are already there.

I once tried to do a obstacle avoidance script on NPCs with raycasting and it made the game unplayable after about 20 ships spawned into the system so I am imagining what horrors may await trying to do the above on a larger scale. Appreciate any advise!

Straight distance checks are super cheap and nothing to worry about.

float range = 50.0f;
float distance = (target.position - transform.position).sqrMagnitude;

if(distance < range * range)
{
   // in range
}

Use something like a giant collision trigger around the ship that maintains a list of each ship within range, to avoid looping through every single ship in the entire game.

1 Like

I was less concerned about the distance checks then trying to keep up with the nearby ship checks. The giant collision sphere is probably a good idea on how to handle this though.

On my player controller function I do have a master list that keeps updated as ships come in and out of the system and on the Player Controller it grabs the ships that are within a certain distance and puts it into another list for the Tab Targetting System so I had original thought of doing something like that on the NPC ships as well. I think the collider method may take up less thought on the scripting part to accomplish

This is a little confusing. How is a nearby ship check not a distance check? But anyway for N objects to keep track of every other N-1 object is ~(N-1)^2/2 checks. So for 100 objects its a little under 5k checks which is pretty easily done each frame. Now if you go up to say 500 objects you suddenly leap up 125k checks.

If you start getting into performance issues from the number of objects you can look into the idea of an Octree to divide your space up. Keep track of which objects are in which sections and only have objects check versus other ships in their area (you might have to add some complexity for ships near the edge). But overall this will greatly cut down how many checks you do per frame.

I’m fairly certain that is how the Physics engine of unity handles things… So tacking on colliders to things and having unity do the heavy lifting will pretty much be the above solution.

My mind was in another place here at work when I typed that.
(then trying to keep up with the nearby ship checks) should be (then trying to keep up with all the ships in the scene) and then doing the distance checks to see which ones were in range.

The large sphere collider may do the trick on that though I use colliders with many other things so I try to limit them where I can to keep down on the confusion of what collides with what, but that is another story for another time.