Enemy evade player

I’m playing a basic top-down game.
I can get the enemy to follow the player using pathfinder, but I want to create an “archer” enemy and have them try to run to keep a minimum distance from the player.

Hi, have you already tried something? Just two thoughts: There is a “Distance between two objects”- condition that could be helpful. Or you could also use a hidden sprite-object around your player that marks the minimum distance with which the enemy archers collide.

I’m using object distance to make the enemy follow the player, but to no avail to evade the player.

Again the two options that come to my mind:

  1. If the distance between your player and the enemies is below a specific value you could add an instant force to the enemies. The tricky thing here is that you have to calculate their angle of movement in relation to the player so that they are not moving in direction of the player. It also depends on how your enemies are supposed to move (e.g. in a formation or as single units).

  2. A quicker but less sophisticated approach would be to make a circle sprite with a radius of the desired distance. Center the circle on the player, hide the circle and move it together with the player. If the circle collides with the enemies you could use the “separate objects” action to push the enemies with the circle which looks as if the enemies are evading the player.

There’s the Player.AngleToObject(Enemy) that will give you the angle at which to apply to force.


Wow, yes that’s it. Works perfect.

@artemisapp this is really all what you need for the basic mechanic:

1 Like

Using the angle and applying the force actually makes him pull away.
But I’m trying to use “pathfiding” to create an AI.
The enemy from afar approaches and keeps a minimum distance, I try to catch up to him and he starts to run, dodging obstacles.

I see. I would argue that an AI is not limited to pathfinding though.

That’s something you could achieve with the following, a little bit ugly looking events:

That’s the part where you would use the pathfinding behavior.

Edit: Now I got it, you want the entire movement of the enemies with pathfinding. This is maybe something to get you started:

1 Like

Good idea yours.
I’ll try.
I believe this will work.