Basic Enemy Movement

Before you start, you will need a basic game with walls.

Download the Basic Platform engine here

You will also need a sprite for the enemy and a sprite for an invisible block,

Download Sprites: Enemy | Invisible Block

 

Adding an Enemy to the Game

Before we can start making the enemy move, we need an enemy.

Load in the enemy sprite and make a new object using the loaded sprite.

 

Making the Enemy Move

When the enemy is appears in the level, the first thing we want it to do is start moving. We will tell it to move in a Create Event using the Move in Direciton Action, with this event and action, we can tell it to start moving in a random direciton. We can then control when it changes direction with walls and invisible blocks.

First, make the enemy move.

On the Enemy object, add a Create Event Event Create

On the Create Event, from the Move tab, add the Move in Direction Action Action Move in Direction
This action tells an object to START moving, the object WON'T STOP unless you tell it too!

On the Move in Direction Settings,

Example Move in Direction Settings

You can choose what direction or directions the enemy will start to move in, if you want to always move in one direction, just click one of the arrows. If you want the enemy to choose a random direction, click what directions you want the enemy to choose from. The middle direction is stop.

Choose the Left and Right directions, this example we will tell the enemy to start moving left or right.

Set the Speed as, 3, this is quite slow but these are basic enimies.

 

Now if you put afew enimies into your level and watch what happens when you run the game, you will see the enimies all choose random directions to move in. The enimies will go through the walls because you have not told them to stop or change direction when they hit one.

 

Making your Enemy Bounce off Walls

Open your enemy object and create a Collision Event with the Wall Object Event Collision

We want the enemy to change direction when they hit the wall, this is just like bouncing off it.
In game maker, there is an Action that bounces one object off another, we can use this to change the direction.

From the Move Tab, add the, Bounce Against Solid Objects Action Action Bounce Against
Leave all the settings as they are, if your Wall is not Solid, it won't bounce.

Now test your game again, when the enemy hits a wall, it will bounce off it and move back the way it came.

 

Make your Enemy Change Direction when it gets to an Edge.

If you are making a platformer game, you might have put an enemy on a platform in mid air, if with the code now, it would only change direction if it hits a wall so what would happen is, your enemy would continue walking in mid air! We can solve this problem by making a copy of the wall object but make it invisible so the enemy will bounce off it but the player can't see it.

I recomend adding a new sprite for this so you can tell the difference in level editor, you can download one at the top.

Load the new sprite and turn it into an object.

Unlike the Wall, this object SHOULD NOT BE solid, if it is, your character will get stuck on it.

Open your Enemy Object and create a Collision Event with the new invisible wall Event Create

From the Move Tab, add the, Bounce Against Solid Objects Action Action Bounce Against

Example Settings Bounce Against All

Because the invisible isn't solid, you have to change, against to, all objects.

Now the last thing you have to do is untick visible so the object doesn't show in the level.

Example Visible False

 

That's all the actions you will need, now put two of these walls in your level like shown, on the edges of a platform that an enemy moves across.

Example Invisible Walls

In the level above, the enemy at the top will change direction when it hits one of the red blocks, these will be invisible in your game.
The enemy at the bottom will change direction when it hits one of the walls.

 

Download working example

enemy_basic_movement.gm6