Multiple enemies patrol, but when close enough to player, they go after player. How?

I have 1 enemy that I was able to figure out how to get it to follow after the player when the player is close enough without going through walls in a top down game. How do I get the enemies to patrol or generally move up, down, left, and right in an area that has walls, and then switch to going after the player when the player is close enough? Then going back to patrolling if the player gets far enough away.

Sorry if it’s obvious. I’m still new to GD.

I tried rectangular movement, but it acted very odd and even broke through the walls.

Thanks for any help!

Are the enemies instances of the same object?

You can make a Variable called roamDir for every enemy and
If Variable roamDir of Enemy = 0 Then
Simulate Up Key
If Variable roamDIr of Enemy = 1 Then
Simulate Right Key

And same for other directions
Now
If Enemy is in collision with RoamUp Then
Change the variable roamDir = 0
If Enemy is in collision with RoamRight Then
Change the variable roamDir = 1

And same like this

Compare x and y of your player and the enemy with

They will not be instances. So I assume what ever solution works for one I can just redo for the others, correct?

They have to be instances. When you add an object to the scene, via editor or events, it becomes an instance [of that object].


If they are different objects, then I suggest you put then in an object group, and apply the events to the group name, rather than each individual object name.

Thanks Mixen.

Would the variables all be under “conditions”, and the simulate be under actions?

This is my first game, but I’m starting to understand bits of how all this works.

Does the roamDir make the movement look somewhat random? That would be perfect.
Also, will it automatically switch to following the player since it can already do that, or do I need to add a condition to get it to switch back and forth to following the player?

Oh! I didn’t know you could have object groups. That should come in handy.

Thank you!

Sorry about the mix up about instances. Coming from animating in Blender, I thought by instance, you meant that all the enemies used the same sprite.

I can’t figure out how I’m supposed to implement that. I’m still learning GD. I tried using a value of an object condition variable, but it shows “roamDir” in red which I think means it needs to be changed.

Hi. You can see the Platformer game example. The enemies move using that principle. You can learn how to implement this by looking at how the enemies are made in Platformer game example.

Thank you. I looked at multiple platformer examples, and none had “roamDir” in them.
The basic platformer didn’t have any enemy movement. I looked at the platformer AI, and that only had left and right.

I’m sorry if this is something really obvious and I’m just missing it. I’m not asking for hand holding, I look up everything as much as I can before coming here asking questions, and I just can’t find what you are talking about.

Also, would I be able to use the top down behavior instead of platformer since this is a top down game?

I’ve got a top down game which has a patrolling enemy/guard. The first guard I’ve implemented patrols left and right and, if the player is within range and sight, will chase the player.

I’ve implemented the left and right bit by having 2 sprites (turning blocks) for the guard to ‘bounce’ between. If the guard hits the turning block, then it takes the direction set in the turning block and moves in that direction.

If the player is spotted, the guard stops moving left and right, and activates a pathfinding to the location the player is spotted at. If the guard gets to the destination and can’t see the player again, it’ll return to the patrol path. Otherwise it’s keeps chasing.


Here’s the scene setup :


Here are the scene events related to the movement. First the setting up of the return-from-chasing location :


And then the various stages of moving :


Which gives the following result (I’ve left the turning blocks visible so you see how they work - normally they would be hidden) :

FixedPath

1 Like

Wow! Thank you for the detailed explanation. You also made me realize I should probably use path finding. Currently the bad guy will hug the wall if the player is on the other side. So much to learn. Haha
I am sure if I keep at it, I’ll get there eventually.

Does it feel easier to do this stuff now that you have experience compared to the beginning. So far every time I feel like I’m getting the hang of things, I try something new and get completely lost.

I was hoping to have the bad guys walk up, down, left, and right in a certain area until close to the player, but for now left and right is better than not moving so I’ll attempt to implement this. Maybe also have a different one do up and down.

Heh, it was easy because it’s a copy & paste of an existing project (one of many started, but not completed).


Up/down shouldn’t be too tricky - just change the ChanegX variable to ChangeY, and the left/right bits to up/down. Then you can have one guard that can do all 4 directions. And since the turn around blocks control the movement, you can probably make them go on the diagonal too.

1 Like

@Muzan

I was able to figure out the variable roamDir part you explained, but the part quoted, about the collision is confusing me. It seems I can only use the collision action with objects. How do I implement the collision you are suggesting?