Enemies Follow Horizontally
In this tutorial you will learn how to make your enemy follow an object either left or right, it will only start to follow if the distance between both objects is within a certain range.
What you need
Before you start, you will need a basic game with a playable character and one enemy.
Download a file to work from:
To make your enemy move towards the character is very simple, all you have to do, is determine if the character is to the left or right of the character, if it is to the left, set hspeed as a negative, otherwise, hspeed as a positive.
This is fine, however, if you do this on a large level, the enemy will move all the way across the level towards the object. We can prevent this by only allowing the enemy to move if the character is within a certain range.
This will all be done on a step event because we want to constantly check the position of the character and move towards it.
Checking the Range
First, you need to check the objects are in range of each other and only move when they are, we should first set two variables for this.
You should always set variables on a create event,
| On the Enemy, add a Create Event |
| Use the Set Variable Action from the Control Tab |
Set the variable name as, distance, set the value as 128
This variable will hold the distance between the enemy and the character. We need another two variables to set the minimum and maximum range the objects have to be within in order for the enemy to start following.
| Use the Set Variable Action from the Control Tab |
Set the variable name as, range_max, set the value as 150
| Use the Set Variable Action from the Control Tab |
Set the variable name as, range_min, set the value as 40
You can change this range values as you want, for now, leave it as is
You can change this range values as you want, for now, leave it as is, so when the objects are 150 pixels apart and the enemy is at least, 40 pixels away, the enemy should move towards the character.
All of your moving objects in the game should have a variable that can control the speed you want to to move, objects all have the variable speed, which is how fast it is moving, this is not always how fast you want it to move, we should make a variable for this.
| Use the Set Variable Action from the Control Tab |
Set the variable name as, move_speed, set the value as 2
You can change this speed later if you want, another good reason to use a variable, you can change it mid-game or on the create event rather than everywhere it is used.
Calculating the Distance
Because this object only moves left or right, we should only check the distance between the object's x positions. We will do the checking and moving on a Step Event.
| On the Enemy, add a Step Event |
| Use the Set Variable Action from the Control Tab |
Set the variable name as, distance, set the
value as,
abs( x - o_target.x )
Make sure you change o_target to the name of the object you want the enemy to
follow.
First, this will get the difference between the enemy x position and the character x position. This could give us a minus value which wouldn't be good to use when checking the range.
We can use the absolute function to make the number a positive. This is done by typing, abs then in brackets after it, putting the number to turn into a positive number.
This will give us the distance between the two objects as a whole number, we can now check if the character is in the enemy range before telling it to move.
Checking the Range
Now that we have calculated the distance between the objects, we have to check this is in range. We want to move towards the character if the distance between them is less than the range, so within a set distance. We also want to stop the enemy moving if it is past the minimum range.
On the enemy Step Event,
| From the Control tab, drag in the IF Variable has Value
question action, the settings will appear. Set Variable as, distance Set Operation as, Larger Than Set Value as, range_min |
This first IF statement will check if the distance is larger than the minimum. We next want to check if the distance is within the maximum range.
| From the Control tab, drag in the IF Variable has Value
question action, the settings will appear. Set Variable as, distance Set Operation as, Less Than Set Value as, range_max |
This second IF statement will check if the distance is less than the maximum. This checks that the distance is larger than the minimum and smaller than the maximum range.
Because we want to do a few more actions after an IF statement, we have to put them inbetween Start and End blocks.
| From the Control tab, add the Start Block action. |
Now anything between this Start Block and the next End block action will ONLY happen if the above IF statement is true.
Checking the Character Position and Moving Towards it
After we have checked that the character is in range, we want to now check if it is to the left or right of the enemy. We will do this by checking if it's x position is less than the enemy's, if it is, this mean the enemy is to the left and the enemy should move to the left, otherwise it's to the right and it should move to the right.
Continuing on the Enemy Step Event,
| From the Control tab, drag in the IF Variable has Value
question action, the settings will appear. Set Variable as, o_target.x Set Operation as, Less Than Set Value as, x Make sure you change o_target to the name of the object you want the enemy to follow. |
This IF statement will check if the character's x position is less than the enemy's x position, if this is true, it's to the left.
Now we know if the enemy is to the left, we shoul make the character move left, we do this by setting the horizontal speed.
| Use the Set Hspeed Action from the Move Tab Set hspeed as, -move_speed Make sure you put the minus symbol before move_speed or the character will move to the right.. |
Setting the hspeed will make the enemy move this ammount on the x axis every frame. So now we've set an action for when the object is to the left, we want to set one for the right, we can use the else action which will happen if the above IF statement isn't true.
| From the Control tab, add the Else action. |
The else action will only happen if, target.x is NOT less than the enemy x position, so this is going to mean the character is to the right, in this situation, we want to move right, again, we will set the hspeed, this time, a positive number.
| Use the Set Hspeed Action from the Move Tab
Set hspeed as, move_speed |
Those two actions will make the enemy object move towards the character object. Now, we want to make sure it stops when it's out of range. We do this with an else to the IF statement that checks if the objects are in range.
| From the Control tab, add the End Block action. |
First we need to end the block of code that moves towards the character. We then want to add an else so we can do something else if the obejcts are not in range.
| From the Control tab, add the Else action. |
| Use the Set Hspeed Action from the Move Tab Set hspeed as, 0 |
Doing this will stop the object moving if it is out of range.
Your finished Step Event on the Ememy Objec should look like this,

Download Working Example