1
\$\begingroup\$

I have more than 200 units in a single scene, all of them use Unity's NavmeshAgent to set their destinations to the main target. The problem I'm facing is that some of the units stop moving when the number of units is large, and they only resume moving when the first few units are destroyed.

I have already tried:

  1. Updating the destination only once every 20 frames:

    if (Time.frameCount % 20 == 0)
    {
        // ...
    }
    
  2. Using a coroutine:

    private IEnumerator StaggerSetNewDestination(Vector3 position)
    {
       myNavMeshAgent.SetDestination(position);
       yield return new WaitForSeconds(0.1f);
    }
    

However, none of these solutions seem to work.

\$\endgroup\$
1
  • \$\begingroup\$ When your number of agents gets this large, you may want to switch to flow field pathfinding instead. \$\endgroup\$
    – DMGregory
    Apr 5, 2023 at 21:13

1 Answer 1

1
\$\begingroup\$

When you start talking about hundreds or thousands of agents, A* pathfinding just doesn't cut it... Too many calculations per frame.

One common approach as mentioned by DMGregory is "flow fields"... For each target/destination, create a texture or 2D array and populate each cell with the path direction at that location.

Agents can then look up the value for their current location and head in the right direction.

Example: https://www.youtube.com/watch?v=zr6ObNVgytk

Steering behaviours and/or added randomness can help make the movement appear less regimented.

Another thing to bear in mind is that having a script on n objects means at least n function calls per frame.

Unity attempts to address this with an "Entity Component System" known as DOTS (Data-Oriented Technology Stack).

Fundamentally, the idea is that you have a set/group of things that need to navigate and a single method loops through them and handles the movement.

There's an ECS renderer that can render objects/meshes based on their position data in the set.

This means you don't need an actual game object, which significantly reduces overhead.

NB: ECS is not ready for production yet. There's a preview available that's pretty good and handles all the basics including physics, but advanced functionality that interacts with other systems (like animation controllers) was lacking the last time I checked in.

It may or may not suit your needs. There are also libraries on the asset store that achieve something similar.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .