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

GMS 2 How Would I Do This [Picture]

Discussion in 'Programming' started by mar_cuz, Sep 2, 2017.

  1. mar_cuz

    mar_cuz Member

    Joined:
    Dec 15, 2016
    Posts:
    228
    Hi Guys,

    I want an object, obj_Character, to follow another object, obj_Leader. The thing is, I don't want obj_Leader to mover further than 32px away from obj_Character at all times. The player will control obj_Leader and it must stay within 32px in any direction from obj_Character. Also, when obj_Leader reaches 32px away, I don't want it to stop, just not move further than 32px away while still moving the current direction at the current speed.

    The game is top down rpg like perspective with movement in all directions. I have looked into lenghtdir_x & y but I am stuck as to how I would approach this.

    Any ideas, please help.
     

    Attached Files:

  2. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,224
    Hey, so if you put this in the green dot's end step event, it will constrain its position to remain inside of the circle.

    var _x = x - obj_black.x;
    var _y = y - obj_black.y;
    var _d = sqrt( _x * _x + _y * _y );
    var _scale = min( 64, _d ) / _d;
    x = obj_black.x + _x * _scale;
    y = obj_black.y + _y * _scale;

    What's going on is there is a vector from the black object to the green object. That vector is scaled so that it cant exceed a length (in this case 64 pixels), then that vector is added to the black object's position, and the result is the green object's new position.

    It occurs to me that this might under some circumstances cause a problem with collisions if you've got solid objects that the player is not meant to pass into. In which case it might be a better idea to simply measure where the green dot would be after a movement, and to disallow the movement if it will carry the green dot outside of range from the black dot.
     
    Last edited: Sep 2, 2017
    Wraithious and mar_cuz like this.
  3. mar_cuz

    mar_cuz Member

    Joined:
    Dec 15, 2016
    Posts:
    228
    Hey thanks for that! I give it a go!
     
  4. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    856
    Note: with the way Game Maker handles the execution of object code, you can never know which object will execute its code first (with the exception of create event code, which has an execution structure that is rigidly defined). What this means is that a 'child' object could move towards its parent object before the parent object has a chance to update its location, leaving the two objects out of sync.

    I mention this because you're trying to create an execution hierarchy that will run into this issue at some point.
     
  5. sp202

    sp202 Member

    Joined:
    Sep 26, 2016
    Posts:
    967
    @CMAllen When order of operation becomes important, that's when you use the with() statement.
     

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