I am making a game in Game Maker right now and cannot figure out how to get an objects exact position and have another object move to that position. Can someone please help me?
Asked
Active
Viewed 7.5k times
2 Answers
8
To get an object's position simply use
xpos = instance.x;
ypos = instance.y;
where instance is the instance id (gained through some method, object id can be used if the instance is the only instantiation of the object).
To start moving towards a position you should set the speed & direction:
direction = point_direction(x,y, instance.x, instance.y);
speed = WANTEDSPEED;

paul23
- 8,799
- 12
- 66
- 149
-
I'd like to add that you can *set* x and y like normal variables as well, so you can just set the new position of your instance immediately, instead of gradually moving towards that position, if that's what you want. Example for setting a (giant) foot to the position of a player: `foot.x = player.x; foot.y = player.y;` – Medo42 Jan 16 '13 at 23:32
1
The object position are two variables (x,y)
You can access them as you would do with any other variable (objectName.variable)
So these two would be, as paul23 said:
object.x
object.y
To make the object move towards a point you can better use this built-in function:
move_towards_point(object.x,object.y,speed)
It'll move by speed
pixels every time it's executed so you may want to put it in the Step event.

kikones34
- 371
- 9
- 13
-
2Please note that Game Maker will grab the first created instance of the object this way. Say, if you'd have 2 nets and you tell the ball object to go to the obj_net, it will go to the first created one. Check out instance id's if you want a particular one. – Rob Aug 26 '13 at 23:15