Platformer Enemies AI

3
  • 51 favourites

Attached Files

The following files have been attached to this tutorial:

.capx

platformer-ai-tutorial.capx

Download now 183.03 KB

Stats

9,811 visits, 15,467 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

UPDATE - 2/5/2015

File now fixes the "Idle" animation not being changed if "Idle" animation is not ordered first in Enemy's animations

UPDATE - 2/5/2015

I always see people asking how to code basic enemies that "patrol" an area and/or basic enemies that follow you when you are near them and attack when you are almost beside them.

I would like to share my method.

Create an instance var for enemies called AI (number-type), initial value is 0

RANDOM WHETHER TO WALK RIGHT OR LEFT

AI = 0

Every 2 secs,

For Each: Enemy

-> Set enemy to Idle animation

-> Set AI to choose(1,2)

WALK RIGHT

AI = 1

For Each: Enemy

-> Simulate enemy "Right"

-> Set enemy to walk animation

-> Set enemy Not Mirrored

->Set Platform: Max Speed to 50

WALK LEFT

AI = 2

For Each: Enemy

-> Simulate enemy "Left"

-> Set enemy to walk animation

-> Set enemy Mirrored

->Set Platform: Max Speed to 50

CHANGE DIRECTION TO RIGHT

Enemy has a wall to left

For Each: Enemy

-> Set AI to 1

CHANGE DIRECTION TO LEFT

Enemy has a wall to right

For Each: Enemy

-> Set AI to 2

That's a basic AI. You can set AI = 3 for run right, 4 for run left, 5 for attack, 6 for hurt, etc. if you want more complex AI.

For run and attack, I suggest using a var to store every tick the distance between you and every enemy so each enemy will calculate its own unique distance from you, then use that distance variable to determine when enemy will attack (for example X<50 when close range attack, x>150 when long range attack). Differentiate Run from Walk by the speed (adjust both max speed and vector x for both run and walk). Of course animations of Run and Walk should be different, or at least different in animation speed.

Create an instance variable called EnemyXDistance and EnemyYDistance

DISTANCE CALCULATION

Every Tick

For Each: Enemy

->Set EnemyXDistance to abs(Enemy.X-Player.X)

->Set EnemyYDistance to abs(Enemy.Y-Player.Y)

I decided to make X and Y distance variables because there will be times you also need the Y distance (ex. if the attack is upward, or when you want to attack only when they are near you, vertical axis-wise). Remember, the distance calculation we put into the instance variables were calculated using the "abs" which means it doesn't tell us if enemy is at player's left or right, top or bottom. For that issue, we need to use an additional condition (Compare X and/or Compare Y) as shown below:

RUN RIGHT

AI = 3

-> Simulate enemy "Right"

-> Set enemy to walk animation

-> Set enemy Not Mirrored

->Set Platform: Max Speed to 100

RUN LEFT

AI = 4

-> Simulate enemy "Left"

-> Set enemy to walk animation

-> Set enemy Mirrored

->Set Platform: Max Speed to 100

WHEN TO RUN RIGHT

EnemyXDistance > 20

EnemyXDistance < 100

EnemyYDistance < 20

Enemy.X < Player.X <---this means the enemy is at player's LEFT side

For Each: Enemy

->Set AI to 3

WHEN TO RUN LEFT

EnemyXDistance > 20

EnemyXDistance < 100

EnemyYDistance < 20

Enemy.X > Player.X <---this means the enemy is at player's RIGHT side

For Each: Enemy

->Set AI to 4

EnemyYDistance < 20

^

I used this condition above to make enemy run if player is at 20 or less. This means you give a bit of leeway for distance calculation. It also means enemies can run in slopes/angled floors (if there are any) when y distance of player is 20 or less.

The good thing about this method is you can expand your AI system by assigning/adding a new value to AI and you don't need to completely recode the older codes (you would only do minor changes if there are changes needed). So if you want a jump, a teleport, hurt/damage, or any other thing that might happen to your enemies, just assign a new value that you haven't used yet.

Since AI = 0 is the default value where you make enemy walk (left or right), always set the AI back to 0 after doing something (after attack, after hurt animation finishes, after teleport, etc. you get the idea).

Recap:

AI values

0 = Idle (randomize walk left/right)

1 = Walk Right

2 = Walk Left

3 = Run Right

4 = Run Left

Optional:

5 = Jump

6 = Hurt/Damage

7 = Attack

8 = Guard/Block

9 = Teleport

and so on.

Always remember to use "For Each: Enemy" on every single event of AI to make each enemy behave independently from other enemy instances.

Question:

So how will AI = 7 be used for different attacks (like for example, attack 1 is a punch and attack 2 is shooting bullets)?

Answer:

I will update this tutorial explaining how to implement it. Please check this tutorial from time to time.

I will try to make a capx in my free time asap but you guys can try it yourselves. It's easier to learn when you code it yourself. This is just a basic AI and it is pretty straight forward.

Please favorite this tutorial if this helped you.

Please comment if you have any problems. I'll try to help as much as I can.

.CAPX

platformer-ai-tutorial.capx

Download now 183.03 KB
  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hello! I know this is really old, but it'd be really useful if you could make the attack .capx file. So if you want to and if you have the time, could you consider updating this?