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

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