• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker How Would I Do This [Picture]

mar_cuz

Member
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.
 

Attachments

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:

mar_cuz

Member
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.
Hey thanks for that! I give it a go!
 

CMAllen

Member
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.
 
Top