# How to Prevent Enemy Overlap

I am working on a 2D side-scrolling action platformer.

I am currently working on the state logic for my enemies. These are humanoid enemies that walk on the ground

They currently have idle, patrol, chase, engage, and attack states.

The default state is patrol, where they walk back and forth on a platform. If they encounter a wall or cliff, they idle for a few seconds then turn around and patrol in the other direction.

If the player comes within a chase_radius, they start chasing the player. If the player comes within an attack_radius, they enter an engaged state where they attack every few seconds.

Currently, I don't have enemies colliding with one another. I allow them to overlap each other. This is nice in the event that two enemies are patrolling in opposite directions. They simply walk passed each other.

The problem is that when two enemies are chasing a player, when one enters the engaged state, they stop and idle until they attack. The second enemy continues to walk until they too are within attack range. They then stop directly on top of the previous enemy and idle until they attack.

In this case, the enemies pretty much completely overlap and it turns an interesting situation with two enemies into a situation with essentially one enemy.

What are some techniques or design choices I can make to prevent this ugly overlap? Ideally, I'd like the enemies to be able to walk passed each other when they are not engaged, but to do something different during combat so that they don't end up overlapping.

I have access to all the normal stuff (for example, raycasting, collision boxes, etc.).

Similar Questions:

1. Enemies overlapping (involves pathfinding, and isn't applicable to 2D sidescroller where enemies walk on ground.)
2. How to avoid enemies overlapping each other when chasing player in Unity? (solution of gradually pulling enemies apart when they overlap won't look natural with humanoid enemies that do melee attacks).

Have them collide with each other using collision geometry such as boxes and circles, but only when they are in chasing or even engaged state. You definitely need some collision detection mechanic to prevent overlap.

if you only want them to not overlap visually, then you can just do this:

private void avoidOverlap(Enemy other) {
if(this.x == other.x) { //or in some radius of this
this.renderx += 3; //renderx is the position where it is rendered
}
}


or you can substitute renderx with x to actually change the position of the enemy. note that this could bring somee problems such as not being reachable with short - ranged weapons or, in the case of other objects that deal damage to the enemy (e.g. spells, falling objects, pets...) it may pervent them from actually hitting the enemy

In my game, I use a couple of techniques, and they are fairly simple and quick to implement.

Each round, as I loop through my list of enemies. I have them register with a collision manager which basically places them into buckets. These buckets are regions based upon the co-ordinates of the enemy in question. As they register, I check that their is nothing else in the bucket, if there is I know there is an overlap of some sort. I then tell the enemy unit to change course for a predetermined period of time. This is one mechanism for removing the overlap, and it works well for my game.

Hope this helps.