How do you keep 2D platformer AI from bunching up?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By behelit

Say you have two enemies coming at you from the right on a 2D platformer. With the collision mask on, the first will engage properly but the second will get hung up walking behind the first enemy. With the collision mask off, they will simply overlap and occupy the exact same space. Using something like the distance_to function, what is a good method to prevent this “bunching” behavior? I would like to turn off the collision mask as well, making their separation purely logic driven. Is it possible to “queue” enemies in such a way that the following might be possible? -

1st enemy: approaches and attacks.
2nd enemy: approaches and attacks from the opposite side.
3rd enemy: approaches and gets behind the 1st enemy (idle).
4th enemy: approaches and gets behind the 2nd enemy (idle).
etc.
etc.

Can someone give me some pseudo code for what that might look like? I’ve tried simple RayCast2D detection, but the results are always buggy and not what is expected.

:bust_in_silhouette: Reply From: sinistro25

You could put another collision shape on a separate layer that only marks the collision between two enemies. Using that way the player still has the collision with enemies as expected and you are free to choose a shape where they collide and overlap in the way you want.

Bah, I’ve tried making it collider based, but it’s never satisfactory. I’ve come to the conclusion that I’ll need to write an entire system to make this work properly, complete with independent variables stored on each enemy and the player… with all actors communicating with each other. Whiteboard time.

behelit | 2020-09-12 23:24