Top Down Enemy AI (Patrolling and Aggression System)

Hey hey! :slight_smile:

I just finished making the basic AI for my game, and decided to share it in case you might need some help making yours!

This AI has two parts…

  • The normal state, where the enemy just wonders around
  • The aggressive state, where enemy chases down the player

To start…make a Global Object Group and put all your enemies inside.

Now lets handle collision…

Make a Global Group for all the tiles you want the enemies to collide against. In my case, i made a group for my “Roof Tiles” and one for my “Wall Tiles”.

Now lets program the events… This part is quite simple, all we have to do is use the “Seperate two objects” action. Use the picture bellow as reference.

…and finally lets do the AI!

First, the patrolling AI

No real need for explaining this stuff, its all timers and object variables… No variables were pre declared, so you can just copy the events as they are and it will work.

The only things to note here are…

  • The “RogueInRange” variable at the top, dont skip this, since this is the variable that will toggle between the “Patrolling” and the “Aggressive” state.
  • The other thing is… Right at the top, we start with a “Repeat for each Enemies”, this is also very important to make each enemy unique.

Next lets do the “Aggressive” state…
Part 1

Part 2

So whats going on here?

  • First… the “BlueJelly” is an Enemy and “Rogue” is my player character

  • We are still under the “Repeat for each Enemies”, this is important

  • If the playes distance goes bellow 250px, the enemy goes into the “Aggressive” state and chases the player

  • If the players distance goes above 450px, then the enemy goes back to the “Patrolling” state

  • All of the sounds, Tweens and Texts are just “Juice” or “Game feel” that i added in, like when the player gets close enough, the enemy will play a sound and get an exclamation point ( ! ) over their head, but when the player runs away, they get a ( ? ) instead.

  • Obviously, you dont need the sound and effects… i only left them in to show you what else you could do with this system to make it more fun!

If you need any extra help understanding any of this just let me know and ill try to explain it a bit better :smiley:

If you enjoy things like this or would like to see what games im currently developing, check out the YouTube Channel!!