1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

How to track movement towards a point

Discussion in 'Programming' started by YamboGames, Dec 4, 2019.

  1. YamboGames

    YamboGames Member

    Joined:
    Nov 27, 2019
    Posts:
    3
    I want to build a check into my game that triggers, when the Player moves towards an object.
    I want to compare the previous distance_to_object with the "now" distance_to_object. If the distance got smaller aka the player moved closer to the object, then it shall trigger.

    How do i manage this?
    Thanks in advance
     
  2. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    894
    you could do this:
    Code:
    var lastDist = point_distance(xprevious, yprevious, target.x, target.y);
    var currDist = point_distance(x, y, target.x, target.y);
    if(currDist < lastDist) {
          // you moved closer
    }
    
     
    YamboGames likes this.
  3. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,220
    You can perform 2 distance checks in the same step and save yourself a lot of work.
    One distance check is before both objects move and the other is after they moved.

    To get a precise result, you may use point_distance()
    Code:
    var last_dis = point_distance(x, y, obj_target.x, obj_target.y);
    
    // move and collision code (code where x and y are added to)
    
    var new_dis = point_distance(x, y, obj_target.x, obj_target.y);
    
    // compare distances now
    if (new_dis < last_dis)
    {
       // objects are closer, trigger code here
    
    }
    
    This will only work if one of the target object is stationary, otherwise, you may have to work with pre_step and post_step events.
     
    YamboGames 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