How do I make NPC follow me in Gamemaker Studio 1.4

2.7k views Asked by At

I've been trying to get an NPC, in my case a zombie, to follow me in my game of course to no avail. I'm using the following script for my character to move

if(keyboard_check(vk_left)){
    sprite_index = spr_player;
    x -= 4;
}

if(keyboard_check(vk_right)){
    sprite_index = spr_player;
    x+= 4;
}

if(keyboard_check(vk_down)){
    sprite_index = spr_player;
    y += 4;
}

The name of the NPC object is (obj_zombie).

2

There are 2 answers

0
klys On

Looking at the simple system you are using to move player object you may just make another object follow to another object (object to object) using move_towards_point(x,y,speed); function in step event of the object zombie in this way:

move_towards_point(obj_Player.x, obj_Player.y,5);

Where obj_Player is of course the name of you player object, which i dont know which is it so i just put obj_Player, you should change it to match yours.

To change the sprite to the direction its moving you can just check the direction the object is going and depending of then the sprite may change or the scale of the sprite may change.

if (direction > 90) and (direction < 270) {
   // left direction
} else {
   // right direction
}

Note: Links over some text in this answer redirect you to game maker studio reference code documentation.

0
Envy On

As Jr Jimnz said, in this case it's better if you use move_towards_point(x,y,speed), personally, that's the system i use in some cases, too! or else, you could use the sign function:

 if (instance_exists(//insert your player object here)) {
 x += sign(//player object.x - x);
 y += sign(//player object.y - y);}

but the sign system is not-so convenient.

sorry for my bad english.