# Is there a simple way to stop enemies standing in the same spot?

So: top-down game, my enemies chase the player, when they get within a certain distance they stand still and fire. If they're all coming from the same direction they all end up standing in the same spot (i.e. standing "within" each other), as I'm not currently doing collision detection between enemies - they are free to pass over each other.

What's a simple way around this? Either some form of collision detection or some ai?

• Limit yourself to one enemy. :P Commented Aug 11, 2010 at 20:20

You don't need collision detection, but you will need to give your enemies a bit of intelligence, and have them avoid running in to each other. Collision detection without that will just make your enemies look stupid anyway - people avoid each other, they don't generally collide.

Look up "flocking" for some simple behaviours.

The basic idea is that things should head towards the player, but away from a close obstacle (each other, walls, that kind of thing).

• It's called also called boids. Read about the simple rules surrounding boids here: en.wikipedia.org/wiki/Boids Should be trivial to implement these restrictions. Commented Aug 10, 2010 at 12:34

You could inspire yourself on flocking behaviours, such as Boids.

These tend to consist on 3-4 very simple rules that, applied correctly, result on very satisfying and "complex-looking" behaviours.

In your case, you could try something like this:

• Separation (minimun radii between enemies)
• Cohesion (so they travel in groups)
• Alignment (so they tend to move on the same direction)
• Targetted (they should tend to move towards the player)
• This is nearly a verbatim copy of JasonD's answer. I swear I didn't look at it before posting. Commented Aug 10, 2010 at 16:19

What you're asking about in general is dynamic avoidance between actors. You could take a look at velocity obstacles: http://gamma.cs.unc.edu/RVO/

You may want to check out collaborative diffusion (great animations here) or some other kind of antiobject pattern for movement. While the diffusion algorithm in and of itself may not stop enemies from crossing over, you can add additional constraints to make it unlikely or impossible.

• Full disclosure: pretty sure I found the collaborative diffusion link here somewhere on GD, but I can't for the life of me find the post that refers to it now. It was quite a find. =) Commented Aug 10, 2010 at 23:30
• This is super awesome but I bit more complexity than I needed. Will read the whitepaper anyway but managed to implement simpler solution.
– Iain
Commented Aug 11, 2010 at 16:14

What if you disallow moving through other units? It seems to me you will need it sooner or later. Whenever the current path is blocked by a unit, adjust the path to move around that.

There's some reference for steering and flocking behaviours at Craig Reynold's website. They're relatively simple to implement but can produce some lovely emergent behaviour.

• This is essentially the same answer as the other 2, but +1 for the link - That website is a really good reference. Commented Aug 10, 2010 at 17:50

All the other answers produce a better result, but the cheapest(?) way to avoid complete grouping is to just add a random vector that is perhaps 25% of the original calculated movement vector so that objects bounce around a bit in a general blob group. It won't keep things completely off of each other, but depending on what you are trying to achieve, I have found this can be adequate. Objects can still group with this method if you run circles around them, but over time they tend to diffuse.

Here is a small game-let I made several years ago that demonstrates this method: http://deleter.phatcode.net/index.php?page=projects&p=4

People always bring up boids because that gives very good results, but implementing boids may be overkill for your specific purpose. In the past I've gotten very good results by simply randomizing the target position within a small circle.

For example, for a demo I built in Unity I used its command Random.insideUnitSphere to do flocking puffballs

• +1 I agree, another example of this strategy (with probably a little better guarantee of not overlapping) can be found here: “Unclutter” units in RTS game Commented Nov 11, 2013 at 23:20

I was facing the same probelm when I using NevMeshAgent as enemy, enemeis are penetrating in eachother so i increase the