How to make an enemy follow your player

advertisement
How to make an enemy follow your player
So, the first step is to make a player sprite (if you don’t have one already). It
doesn’t matter what it looks like this is just a tutorial. After that just make a quick
wall sprite.
Now, we need to make an enemy sprite. This involves 2 sprites, one that goes
left and one that goes right. Check the image below just for example of an enemy
going both ways. Make sure you center both of them. On the enemy right call it
Enemy_R and for the enemy left call it Enemy_L You will see why later.
Enemy right
enemy left
Make all your sprites into objects. Then go ahead on the player and add
movement, gravity, and collision with wall.
I’m sure you know to set the wall to a solid.
Now for the enemy.
Go into the enemies object settings and add the step event exactly like the
player, add gravity to the enemy too.
then, add a script in the step event of the enemy. It is the icon that looks like this
image
.
Now, type in this piece of code-
if distance_to_object(Player) <100 //you can always change the
100 to whatever satisfies you
{
sprite_index=Enemy_L; //use only if you have a left & right
enemy
move_towards_point(Player.x,y,2)
};
//use only if you have a left & right enemy
if hspeed < 0
{
sprite_index=Enemy_L;
};
else
{
sprite_index=Enemy_R;
};
That’s it you should be almost ready to play the game. Just make sure you have
a collision with wall event on your enemy to.
Test out game once you have made the room and the player, walls, and enemies
in the room.
Download