Enemies, how to prevent them from fighting on top of each other

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

Hello! I am making a game where the player 1 generates an army from one corner and player 2 generates another from another corner using KinematicBody2D.

Both armies follow a route and detect the enemy.

When the soldiers are generated, if it is done very fast, it is one on top of the other and it looks bad. If they are generated with a cooling it looks better although at the moment of fighting they remain one on another.

How to make each soldier keep his space and surround his companions?

I am new XD I appreciate any solution or advice!

:bust_in_silhouette: Reply From: njamster

Sounds like your enemies either don’t have a CollisionShape2D or are moved directly (by changing their position- or global_position-properties) instead of using the move_and_slide- or move_and_collide-functions that check for collisions.

If you haven’t already, I recommend you work through this tutorial which will teach a lot of important fundamentals of developing games with Godot.

Hello! Thank you very much for the quick answer!!

The Army scene is a KinematicBody2D type node and has the nodes:

Sprite
CollisionShape2D

The KinematicBody Army on its PhysicsBody2D property:
Collision Layer:
Player
Collision Mask:
Enemy, Obstacles

When I run the Army and Enemies game when they touch they keep their distance. The problem is that Army units are piling on top of each other.

How do I make them respect their CollisionShape and surround their teammates to attack the enemy?

gib_2004 | 2020-04-18 20:17

The KinematicBody Army on its PhysicsBody2D property:
Collision Layer:
Player
Collision Mask:
Enemy, Obstacles

That means that Army units are on a collision-layer “Player” and can collide with nodes on the layers “Enemy” or “Obstacles”. However, then cannot collide with other Army units, as these are only on the “Player”-layer which is not part of their collision mask. If you add the “Player”-layer to their mask, it should work.

njamster | 2020-04-21 16:22