Unity NavMesh Make multiple enemies take different paths

Andrew picture Andrew · Oct 30, 2014 · Viewed 7.3k times · Source

I am working on a game where I have multiple enemies all moving towards the same target. The problem that I have is that all of the enemies are taking the same path to get to this target, which results in an unrealistic "bunching" of the enemies. My question is this: how can I get them to spread out a little bit to produce a crowding effect? I am currently using Unity's built-in NavMesh system. I know there must be a way to penalize the path that is used by one enemy to encourage the other enemies to take alternate paths, but I just do not know how to implement it. Any help is appreciated! (also, I understand code best in c#, but I can somewhat understand JavaScript)

Answer

maZZZu picture maZZZu · Oct 30, 2014

You can set randomly different Radius to each agent. Then they will take different routes around obstacles. This will not change the fact that in empty space they will all behave the same.

Maybe you could also introduce little randomness to the destinations. But if there is too much randomness it can make the agents look stupid.

Edit:

For some game designs, a good approach might be doing navigation for a squad of agents and have position offset, from the center of the squad, for each member in it. You can also form a squad dynamically if agents get close to each other.

For finding if agents are so close to each other, that they should dynamically form a squad, you don't necessary need to loop trough all other agents in every update of each agent.

First of all, depending on the intensity of the game, testing the distance can be done on every 10-100th update or so.

Secondly you can use special data structures to store references to all the agents. For example if the game area is fixed it can be divided into equally sized areas. There can be 2D (or 3D) array of vectors where all the agents in the corresponding areas are stored. Then an agent needs to test only other agents that are also referenced in the same vector or in neighbor vectors. Of course updating the references are needed when agents are moving.

And if there is already agents in a squad, it might be enough to test only against the squad, not against every single member in it.