[SOLVED]How do I make enemies avoid the player when either of them reach a certain distance with one another?

So I have been messing about with pathfinding and boids movement recently but I can’t find a video tutorial teaching you how the AI can stay at a distance when it reaches a certain amount of distance towards the player.

One solution for me would be boids as it has the events that can tell an object to avoid something, but but thing is I dont want all my enemy objects to move like a flock.

Can you describe the theme of your project or share a screenshot of the scene?

You can use an object boolean variable as a mode. Start with th object variable seek to true . If the distance between the objects is below some number then set seek to false and then the enemy can pace or patrol a small area around the player. Maybe hide behind something or go to a preset location or just random nearby points . It’s tough to say without knowing what the theme of the game.

Something like
Enemy boolean seek is set to true by default

If seek is true chase player

If seek is true and distance between objects is <100 then set seek to false

If seek is false then pace or patrol or hide IDK

If seek is false and distance > 150 then set seek back to true. This would give some wiggle room so it wasn’t switching back and forth between modes.

My project is a top down game, and currently I’m using pathfinding behavior on the enemy object.

I’m currently just using a prototype project so I can do whatever I want here but anyway this is the only thing inside my events, and I dont know how to make it stop at a certain distance, I just want the enemy to stop where it is and no need for it to patrol or anything like that.

To make it stop, you either disable the behavior or set the target location to the current position. I wish there was a cancel move.

If dist < some # move enemy to enemy.CenterX() , enemy.CenterY()
Add a trigger once

The current even might need something like
If dist < 500
If dist > 100

I’ve found a way to make the enemy stop when the player is above a certain distance but I still can’t make it retreat when the player gets too close to it

You would have to pick a location that’s farther away. It becomes complicated if there are obstacles then you need to check for a valid location.

I tend to over think things like this.

You could have it retreat to a set location or a random locations but the player might be blocking that location or in its path. Or the target unreachable. There’s probably some logic that could be used to pick the nearest.

It could always temporarily get closer to reach a safe location.

You could base it on the angle between the player and enemy.

I honestly don’t have an answer. The enemy doesn’t normally retreat. It’s an interesting concept predator becomes prey. If backed into a corner it could lash out.

I know that boids movement has an actual command that lets it avoid an object but I dont want to rely on that extension too much.

The channel Gdevelop in youtube can have his enemies retreat when the players get close in some of his videos but he doesn’t show the event for it and I’m sure he doesn’t use boids movement in it or rather if he has boids in them but he’s not using the boids movement to avoid the player.

Here’s an approach that works. It might not work in your situation. If not, it might help.

Try me: The player is draggable. The enemy path is drawn with the draw pathfinding behavior assigned to the enemy object.

The concept is pretty straight forward. When the enemy is too close to the player. It gets the angle between the player and enemy and adds a random degree to it from -60 to 60. It then picks a location in that direction at a distance of 500 from the player.

This could end up cornering the enemy if there’s nothing but obstacles behind it. You can use an inverted path is found for the pathfinder to either increase the angle variance or just start picking random x,y locations. Alternatively. the enemy could remain still or attack directly like a cornered animal.

You might want to reduce the pathfinding resource usage by waiting for the enemy to reach its destination. There’s a condition. Or use a timer and only refresh the location every second or 1/4 seconds. It depends on what else is going on in the scene.

If there are multiple enemies you might need a for each enemy plus the distance checks less than or greater than but not equal. You might need to use compare 2 numbers and check the distance manually or something.

Sorry for the late reply been busy with other stuff on my project.
Now anyways I’ve tried this on my game and it works! but I only use it for a few enemies and majority of them use boids, and I greatly appreciate the help with this and thanks!

1 Like