Enemies Follow like Ghosts

Before you start, you will need a character that can move around a level.

Download the Basic Movement engine here
Download the the Basic Platform engine here

Download the Ghost Sprite here

 

In this tutorial, you will make an enemy follow your character around like a ghost. This means the enemy can move in any direction to get to your enemy and will move around walls that are in it's way, if possible. This enemy will hunt down your character.

Another thing that this enemy will do is ONLY follow your character if it is in a certain range. Why? Because if we left it without checking a range, if you ghost was all the way on the other side of the level, it would go all the way through the level to get to you. This way also lets you out run the enemy.

 

Creating the Following Enemy

First you need to create an enemy that will follow your character, you can use the ghost sprite for the example.

Create a new object and call it, o_enemy_follow

You need a few variables on your enemy,

- Distance from the Target Character, used to check if in range
- Following Range, basically, how close character is before enemy follows
- Following Speed, how fast your enemy follows the character

First, you need to set these variables, the place to do that is a Create Event,

Add a Create Event to your enemy character Event Create

From the Control tab, add the , Set Variable Action Action Set Variable

Set variable as, follow_speed
Set value as, 3, or how fast you want the enemy to move.

From the Control tab, add another , Set Variable Action Action Set Variable

Set variable as, follow_range
Set value as, 150, or how close you want the character to get before the enemy follows.

From the Control tab, add one more, Set Variable Action Action Set Variable

Set variable as, target_distance
Set value as, 0, this will be updated in a step event, how far your character is from the enemy.

 

Making the enemy follow

Now you have set all the variables, we can start making the enemy follow the character.
Before the enemy can follow, the character has to be close enough.

First, we have to get that distance using a little bit of advanced code to set the target_distance variable.
To run typed code in game maker, you have to use the Execute a Piece of Code Action.

 

We have to keep making the enemy check and follow the character so we need to do the actions in a step event.

Add a Step Event to the enemy follow object Event Step

From the Control Tab, add the Execute a Piece of Code Action Action Execute a Peice of Code

The Execute a peice of code window will pop up, this is where you type in code to run.
The code runs just like an Action Block but you have a lot more control over what happens.

Example Code

In the window, copy and paste this code in,

YOU MUST CHANGE o_character TO THE NAME OF THE OBJECT THE ENEMY SHOULD FOLLOW!

target_distance = distance_to_object(o_character);

Basically, the first part of the code is the variable name, target_distance.
The equals sign means, set the variable as whatever is after the equals sign.
distance_to_object(o_character); is an Action but in words rather than a block, in the brackets is the objet we want to know the distance to.

Once you've pasted or typed that in, click the tick at the top left.

This code basically replaced the Set Variable Action, we had to use this because we can't use the distance_to_object action when setting a variable with the action block.

 

Now we have the distance, we should check it to see if the character is close enough to follow.
We want to check if the target_distance is SMALLER THAN the follow_range, if it is, then it's ok to follow.

From the Control Tab, Add the, Check IF Variable has a Value Action Action IF Var has Value

Example Check Range

Set the variable as, target_distance, we want to check the distance between the enemy and character.

Set the value as, follow_range and operation as smaller than, we want to check the distance is within the range.

 

There is an Action that moves an object towards a point while avoiding things like walls, this is the, Step towards a point avoiding objects Action.

From the Move tab, add the, Step towards a point avoiding objects Action. Action Step to Point

We want to move the enemy towards the characters position which is the x and y co-ordinates of the character object.

Example Step towards Character

YOU MUST CHANGE o_character TO THE NAME OF THE OBJECT THE ENEMY SHOULD FOLLOW!

Set X as, o_character.x
This tells the Enemy to move towards the x point of the character object

Set Y as, o_character.y
This tells the Enemy to move towards the y point of the character object

Set speed as, follow_speed, this is the variable we set on the create event.
The advantage of having this speed in a variable is so we can change it during the game if we want to.

 

Put the enemy object in your room at one side and the character at the other.
When you start the enemy should stay still, when you get close enough, it should start to follow you until you've out run it.

If you want a challenge, try chaning the sprite when the enemy is following your character and back to normal when it's not.

 

Download working example

enemy_follow_ghost.gm6