# Prevent instances from overlapping gamemaker studio

I have a game where multiple instances of an enemy move towards a player in their step process. There can be as many as 50 instances on screen.

The issue is that the instances all end up in 1 big group as they follow the player. I would like to prevent them from getting more than 2 pixels of each other.

the code is as follows:

if distance_to_object(player) < 160 {
direction = ppoint_direction(x,y, player.x, player.x, player.y)
p_potential_step(x,y, player.x, true);
}


If I put a distance check of 2px in this code, the instance starts making jerky movements and spins. I would like the instance to continue following the player, but just not allow themselves to overlap.

Inside this event, make the enemy step into a direction where there is space. You can determine that via place_free for example. In my case of a space game, it is rather easy since I can just let them step in any direction until they have enough space to operate.
x = xprevious