GML Making NPCs avoid eachother? (or zombies)

Dr_Nomz

Member
So I want NPCs to be able to move all at once towards another NPC or the player, depending on it's target, but I don't want them all being stuck together. How can I make them avoid eachother? (Like having them each have a small invisible forcefield, that'd be awesome.)

Currently I use this:
motion_add(direction,speed/2);
But it does like nothing, and they only seem to separate if the PC moves THROUGH them, which isn't good.

I use a path/mp_grid system, if that helps:
^Just like the one in the video
 
N

NeonBits

Guest
Would it work if you add in step event a line saying something like:
Code:
if ((obja.y < objb.y) && (distance_to_object(objb) < 5))
{ with (obja){y += 5;}}
else
if ((obja.y > objb.y) && (distance_to_object(objb) < 5))
{ with (obja){y -= 5;}}
I know, it's not fancy. Or you could set the speed to zero instead and add "else" to restore the speed. This checks only if an object is above or under, need to add x coordinates. Note: I haven't watched the vid.
 
Last edited by a moderator:
You'll have to look into avoidance behaviours. It's a complicated topic, made more annoying by the fact that GMS doesn't natively support vectors. Basically, you want to find the central vector of ALL the instances you want to avoid and then move in the opposite direction of that. PixellatedPope has some flocking behaviours that include avoidance in a tutorial or scripts or something somewhere, so search him up and have a look.
 

rytan451

Member
If, in each step, the zombies were to check the distance between each other, you could get them to avoid each other by moving away from each other depending on the distance between them.
 

NightFrost

Member
Pathfinding with dynamic collision avoidance a complex subject and there are no simple solutions to it. It sounds like you want a multitude of enemies to converge on a single target. For that, flow field is likely the best pathfinding method. This part will tell your enemy entity which way they want to move any given moment. For dynamic avoidance, steering behaviors. You combine flow field's desired movement direction with avoidance forces from SB to arrive to movement direction that is optimal and will (eventually) take you to target position. I'd recommend making separate projects where you experiment with these to get familiar with the mechanics, before you attempt to drop them into existing projects.
 
Top