Reputation: 33
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)
Upvotes: 3
Views: 10284
Reputation: 933
I would create a GameObject called "Formation". Make as many child objects as needed within this formation object to simulate the desired enemy placement position. Place each child placement GameObject as tightly-knit -or- as sparsely populated as fit your needs. Sparse population (greater distances from the center of the formation GameOject) alone will cause the zombies to take different paths through obstacles simply because their shortest distance will change as you move the formation GO.
For each enemy zombie in this case, have the zombie's NavMeshAgent target become their corresponding GameObject placement inside the formation GameObject. Placing a NavMeshAgent on the formation in this scenario seems to produce some undesirable jiggling of the zombies. However, simply moving the formation slowly towards the target play seems to produce reasonable 5-minute results.
If you want to add variation to your formation, perhaps to add a mob mentality simply add a "Bob" type of script to each formation that lerps the zombie's target placement in the formation a little bit off-center, etc.
This also makes it trivial to swap enemy roles in a squad by simply switching their target position in the squad (for other game types).
Upvotes: 2
Reputation: 3615
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.
Upvotes: 4