How to AI by example - Platform follower

Favourite 16 favourites
Tutorial written by ValerienOriginally published on 9th, September 2014 - 2 revisions

In my latest tutorial, I gave an introduction to Finite State Machines, and how critical they are as far as AI programming is concerned. This capx-based tutorial offers an example of a platform follower AI, with and without using an "elegant" Finite State Machine. And if you like what I'm doing, you can follow me on twitter and on facebook!

The capx comes in 2 versions:
1- The first one offers one of the most basic platform follower behavior one can make, as well as a captor-based, more elegant alternative.
2- The second capx is simply a re-factored version of the captor approach, using rex's FSM behavior plugin (see links below). I invite you to take a look at it and see how it facilitates the encapsulation or organization of your code.

Installing rex's plugins

In order to open both capx, you'll need to copy the following plugins in your construct folder:
1- The FSM behavior from rexrainbow.
2- The Platform MoveTo behavior from rexrainbow.

Here's a 3 step guide to find and install the plugins:
1- In order to download them, you have to click on the "plugin" link in yellow on Rex's website.
Rexrainbow's website plugin link
2- Then, unzip the plugins and copy the extracted folders to C:\Program Files\Construct 2\exporters\html5\behaviors (The disk or install folder may be different on your machine)
3- You can now open both capx.

If you want to develop elaborate games using Construct 2, or simply make your design process easier when prototyping, I highly recommend that you check all of rex's plugins. He has made a ton of extremely useful tools for you to experiment with.

"Dissecting" the system

This capx example was made after @christina's request.
The AI has a simple behavior : it targets the player's position, and jumps when it "sees" or comes across an obstacle. It only works as long as the level design is rather horizontal. There is no gap, trap or path detection (although the first two can be added easily).

The AI has 2 states: idle and follow. When it's idle, it doesn't move. It enters the follow state if the player gets too far. When it's in the following state, the AI starts moving continuously towards the player on the X axis. A little object, called captor, is projected forward on the AI's path. This captor is used to detect obstacles when the follower is moving. If it overlaps an obstacle, the AI will simply jump. I invite you to look at the code directly in order to get per-section comments. The system is rather straightforward.

An alternative to the captor would have been to use the "overlapping at offset" condition in Construct 2. I favor the captor mostly as it offers a visual debugging tool throughout development, and can be re-purposed.

Last words

Would you like me to cover something specific? Drop me a message! You can follow me and ask me questions directly on twitter, facebook and google plus.
You can find all of my articles and tutorials about game design directly on my website: ! Finally, you can find all of my Construct 2 tutorials on this forum post.

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.


justdontmind8 600 rep

Thank you very much! :D

Saturday, April 30, 2016 at 5:25:36 AM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.