How do I make the enemy patrol, chase in a certain distance and at the same time attack the Player when in Collision

Screenshot 2023-06-13 211556

So I really am struggling since 2 days already and I cant find a perfect solution for this. So basically whenever I watch a tutorial its only about 1 topic like for example: How to make enemy chase player but when I combine it with other stuffs like Make the Enemy attack the Player then it’s messing up the code and make it useless.

so basically what I want is a tutorial on this advance thing where the enemy is patrolling then when in a collision check of a certain distance then the enemy will follow the Player and then when they collided, The Enemy will attack the Player and that’ll reduce the Player’s health and add force to it.

then go back to Patrolling after the Enemy is not in the collision of that Player

Hello, Xilentred

Could you share screenshots of the events that you already have of enemy movement and attack?

I’ve achieved this using a Finite State Machine. Although mine is a top down game, the concepts can be applied to platformers.

Each enemy has a set of states it can be in, like walking, turning, spotted player, chasing, checking, returning to walk etc. It looks like this (action is the state the enemy is in):

SpottedByEnemy


Some of the events (collapsed) that achieve this:

In a nutshell, the enemy walks until they hit a hidden turn block, turn and then repeat the process (Walking & Pausing states).

If during the walk they spot the player, the enemy hastens over to where the player was spotted (Spotted and Chasing states).

Once they reach the point where the player was spotted, they check again for the player (Checking state).

If there’s nothing, they head back to the position they were at when they spotted the player and resume the walk > turn routine (ReturnToWalk state)

If the player is spotted, the enemy speeds up and chases the player relentlessly (RedAlert state).

1 Like