NPC Following Help

Discussion in 'Programming' started by ShortyBishop, May 15, 2019.

  1. ShortyBishop

    ShortyBishop Member

    Joined:
    Jun 10, 2018
    Posts:
    7
    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: May 16, 2019
  2. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    221
    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);
    }
    
     
  3. eimie

    eimie Member

    Joined:
    Apr 14, 2019
    Posts:
    12
    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.
     
    woods likes this.
  4. ShortyBishop

    ShortyBishop Member

    Joined:
    Jun 10, 2018
    Posts:
    7
    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.
     
  5. eimie

    eimie Member

    Joined:
    Apr 14, 2019
    Posts:
    12
    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?
     
  6. ShortyBishop

    ShortyBishop Member

    Joined:
    Jun 10, 2018
    Posts:
    7
    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: May 17, 2019
  7. eimie

    eimie Member

    Joined:
    Apr 14, 2019
    Posts:
    12
    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!
     
  8. ShortyBishop

    ShortyBishop Member

    Joined:
    Jun 10, 2018
    Posts:
    7
    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!
     
    eimie likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice