Login | Register

Move towards

January 8, 2007, 16:42 by Calle
[loading]
-->
A lot of people at GMC doesn’t seem to know how to make an object move towards a specified point, that’s why I am writing this article. I will show you two ways, both does exactly the same thing but one is shorter than the other, the longer is the better according to me. Well, you will see. I am going to use the mouse position as position however, but you can change this yourself. Most people who asks wants to use the mouse position for their RTS or RPG or whatever it might be.

First of all I’ll save the position to the variables xp and yp, like this:
xp = mouse_x;
yp = mouse_y;


Then it’s time to get the object moving. This is the *tricky* part, a lot of people seems to believe, but in fact it’s only one function. And it is easy to find in the manual.

move_towards_point(xp,yp,speed);

That’ll work very well. It doesn’t matter if you use the function several times or only one time, because the object will still keep the same speed and direction, but I myself prefer to always only execute functions like this once. I do this because it is unnecessary to do the same calculation a lot of times, when once is enough. It just slow down the game, I will now show what calculation I mean:

direction = point_direction(x,y,xp,yp);
speed = speed;


This is identical to the function move_towards_point; it is the definition itself. You may use it just for the sake of it. Study it and you may all so gain some insight into how Game Maker works, and how to move objects. You simply set the direction and you set the speed.

I hope this article helped you at least a little. You may ask anything you like, go ahead.

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games