- Home /
How many nav mesh agents can I have in a scene ?
All nav mesh agents will be checking the distance of the player from them,the player can control multiple units which also are navmesh agents. So performance wise, say I want to have the game run on mobile platforms, how many can I keep in a single scene? The Nav mesh will be small to mediu, sized ,i.e ,it won't be huge.
Answer by HawkSandwich · Jun 16, 2017 at 08:30 AM
There's no quick answer to this, and it'd even be really hard to estimate.
Vector3.Distance is a very, very costly algorithm to be calculating every frame for so many objects. This is mainly because it requires calculating square root. I can't tell you how many agents you'd be able to have, but I'll give you a little tip that might help avoid using Distance, which will allow you to have many many more agents.
Vector3.Distance really is for precise measurements, but if all you need is "is x within y distance of z?", then maybe you could just check those values manually. As an example, let's start with a one dimensional "version" of this. Let's say you have two cubes: the blue one is the player, the red one is the bad guy. If the red one is within a meter, have it kill the blue guy.
Using Distance, it'd look like this:
Transform blueGuy, redGuy;
void FixedUpdate () {
if (Vector3.Distance(blueGuy, redGuy) < 1f) //kill the red guy
}
Instead, we could do something like this:
Transform blueGuy, redGuy;
void FixedUpdate () {
if (Mathf.Abs(redGuy.transform.position.x-blueGuy.transform.position.x) < 1f) {
//kill the blue guy
}
}
I'm using FixedUpdate here to reduce the amount of times we need to do the calculation, but of course it's still much less costly even when used in Update.
If you do that same calculation for x AND z (and maybe even y, if that's necessary), it'll save your device from the headache of trying to calculate square root every frame for potentially hundreds of objects.
If this isn't a possibility for you, definitely at least consider using your Vector3.Distance in FixedUpdate, that'll near double the amount of agents you can have by itself.
Answer by lclemens · Mar 30, 2020 at 07:52 AM
Is there hard limit on the agent count? The Unity tower defense template uses a navmesh, but if you add over 100 agents, they all start grouping into bunches at nodes. It's as if the pathfinding can only be calculated for 100 or so at a time. FPS stays > 100 or so and cpu is < 20%. Agents are being created and recycled in a pool manager. I've spent an entire day looking for a max-agent-count setting somewhere in the code or in some unity setting, but I haven't been able to find it. Is this limitation in unity or is it some sort of hidden limit in the tower defense code?
i think it's hitting a limit within navmesh. it seems that it takes a long time to calculate paths, so if you spawn 200 agents at the same time and tell them all to move to a specific spot, it'll take 20 to 30 seconds for unity to generate all the paths (it seems like maybe 3 or 4 per second) with no obstacles. I guess unity's built in navmesh was designed for games that need only a handful of agents.
Because it is clock sync'd and costly, it probably runs on 'complete or abandon', or 'complete or queue' logic, instead of 'hold frame to complete'.