The Dave's Game Maker Guide is a compilation of tutorials requested by Game Maker users. If you would like to submit a request, visit this thread at Starmen.net.
Q: I made it so an object will follow the cursor, but how do I make it so the object moves faster the further it is from the cursor?
A: Simple! Like this:
If you have any questions ask me in the thread mentioned above.
A: Simple! Like this:
if (point_distance(x,y,mouse_x,mouse_y) > 1) // if we aren't really close...
{
direction = point_direction(x,y,mouse_x,mouse_y); // face the mouse
speed = point_distance(x,y,mouse_x,mouse_y)/10; // and get moving!
} // divide by ten so we don't go so fast - change this to go faster
else // otherwise...
{
x = mouse_x; // we're basically on top of the mouse, so we
y = mouse_y; // just set the position to the mouse's so
} // the object doesn't keep moving around.
What this does is as long as the distance from the mouse's position is greater than one pixel, move towards the mouse position. If the distance is less than one pixel, just snap to the mouse position, otherwise the object would keep moving in very tiny incriments forever.{
direction = point_direction(x,y,mouse_x,mouse_y); // face the mouse
speed = point_distance(x,y,mouse_x,mouse_y)/10; // and get moving!
} // divide by ten so we don't go so fast - change this to go faster
else // otherwise...
{
x = mouse_x; // we're basically on top of the mouse, so we
y = mouse_y; // just set the position to the mouse's so
} // the object doesn't keep moving around.
If you have any questions ask me in the thread mentioned above.
