NPC Following Help

S

ShortyBishop

Guest
Currently, I'm trying to have an NPC follow my current player character but am having difficulty implementing it. My code seems sound, but when I test it, the NPC simply stands still (but responds to other actions, such as implementing the text bubble that appears above their head).

This code is all in the OBJfriend object- the NPC I'm trying to get to follow my OBJplayer.

Create
Code:
image_speed = 0;
walkSpeed = 3.5;
following = true;
(Following is normally set to false but for this, I simply set it to true, a different object sets this to true. Even when running the code with true, it does not work.)

Step
Code:
if (following = true) and (distance_to_object(OBJplayer) > 10)
{
    move_towards_point(OBJplayer.x, OBJplayer.y, walkSpeed);
}

EDIT: I'd like to include that the style I'm using is top-down, and not side-scroller.


Thanks to whomever can answer this question! It feels excruciatingly simple but I can't quite put my finger on it.
 
Last edited by a moderator:

woods

Member
my first thought..... "a different object sets this to true"
is there somewhere else that it is being set to false?

did you try point_distance instead of distance_to_object?

Code:
if ((following = true) and (point_distance(x, y,OBJplayer.x,OBJplayer.y)) > 10)
{
   move_towards_point(OBJplayer.x, OBJplayer.y, walkSpeed);
}
 

eimie

Member
I tried your code by copy-pasting it in a testproject. It works fine. So it must have something to do with the variable following.
 
S

ShortyBishop

Guest
my first thought..... "a different object sets this to true"
is there somewhere else that it is being set to false?

did you try point_distance instead of distance_to_object?
I tested point_distance and it had the same result as distance_to_object. I ended up adding a little bit more code to verify where the problem was, such as setting image_speed = 1.

It looks like my problem is the fact that OBJfriend just... isn't moving, despite calling distance_to_object.
 

eimie

Member
Keep cool and search systematically. As stated before, your code works.

Try this:

Code:
if (point_distance(x, y,OBJplayer.x,OBJplayer.y) > 10)
{
   move_towards_point(OBJplayer.x, OBJplayer.y, walkSpeed);
}
Moves your object now?
 
S

ShortyBishop

Guest
Try this:

Code:
if (point_distance(x, y,OBJplayer.x,OBJplayer.y) > 10)
{
   move_towards_point(OBJplayer.x, OBJplayer.y, walkSpeed);
}
Moves your object now?
It still does not work. I'm gonna comb through it all again, see if I missed anything in particular.

EDIT: I ran just
Code:
move_towards_point(x, y, 5)
without the if statement in my step event. However, still no luck.
 
Last edited by a moderator:

eimie

Member
Ok, that's weird. Although your edited example from 8:14 can not work (you send your object to the x and y coordinates where it already is), the first one should definitively do the job.

move_towards_point manipulates the variable speed. Perhaps you change it at another place back to 0? To clarify that, I would recommend to fix you oneliner into move_towards_point(0, 0, 5), make sure, that the object is not already at position 0, 0 and run it again.

If that still does not work your problem may be, that you change the objects speed variable somewhere to 0.

Good luck!
 
S

ShortyBishop

Guest
Well, after I kind of gave up for a bit, I started fresh (especially considering I hadn't gotten very far with my project in the first place).

I quite literally put this in my step event:
Code:
if (distance_to_object(OBJPlayer) > 50) move_towards_point(OBJPlayer.x, OBJPlayer.y, spd);
//spd being the run speed

else move_towards_point(OBJPlayer.x, OBJPlayer.y, 0);
And it worked like a charm. I still have no idea what went wrong originally, but now that I got something to work, I'm not necessarily complaining lol. Thanks for your help though!
 
Top