2
\$\begingroup\$

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).
\$\endgroup\$

4 Answers 4

1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

If you are ok with more work on sprites you could simply use different sprites for the enemies.

  1. You could have enemies composted of randomized different parts. This is probably overkill just for this issue, but would be good for user experience in general when applicable. For inspiration, blog about Papers please describes how different persons are generated there. If you have enough randomization and variability of parts no additional logic would be required for overlapping.

  2. Have multiple sprites for attack. If generating different enemies is too much work, you could just use different sprites when enemies overlap. Just make minor changes to attack animations and decide what animation to use when attacking. Or have sprite drawn from different angle. When two enemies overlap just select different animation from other enemy. Could be still issue if more than two would overlap.

Both solutions above introduce visual variability even when not overlapping.

  1. Have some kind of indicator on top of enemy. Many games use some kind of indicators to show type, health, etc. This indicators could be stacked instead of moving enemies itself or could indicate number of enemies.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .