How can I make enemies surround their target instead of blocking each other while doing so

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


I’d like to find a way to make them surround the player.

Theres a collision detection between them so they don’t stack on top of each other, but I’d need them to find another path if an enemy blocks their way.

I read about a* pathfinding / navigation2d, but I think I’d need an example to understand how to implement this. Do you know any?

:bust_in_silhouette: Reply From: Zylann

I don’t have a silver bullet to fix this but last time I had to deal with this problem, I did the following:

  • Made the enemies not collide with each other, at least not in a “solid” way (they can still have areas to detect each other)
  • Add a force to enemies so that they repel each other if they get too close and will be less likely to stack. The direction into which the force goes can be tweaked to be sideways towards the direction the enemy wants to go, so they dont slow down. An inspiration to this is Minecraft, where mobs have “soft” collision.
  • Add some degree of randomness to speed and direction of enemies
  • An enemy cannot block another because whichever blocks them is supposed to also chase the player. But if one still does, then maybe it is static, and so should be considered an obstacle in pathfinding. Or it could be thrown around by the blocked enemy, “let me pass”.
  • Dead enemy corpses have no collision

I did use pathfinding from Godot (checkout the doc and the demo project attached). There might be more advanced techniques around like flocking behaviours but this was enough for me.

1 Like
:bust_in_silhouette: Reply From: jfcartier

I found the solution in this thread : Reddit - Dive into anything

I finally went for a Context-Based Steering concept. Here’s the documentation on the topic :