How to Make Objects Follow the Cursor in Game Maker 7.0

By Kevin Walker

Having an object in Game Maker follow the mouse cursor around the screen can be done pretty easily with a few built-in functions. The most important part is to remember that you can always retrieve the "X position" of the mouse--or the distance of the mouse from the left side of the screen--by using the "mouse_x" keyword. Similarly, you can retrieve the "Y position"--or the distance of the mouse from the top of the screen--by using the "mouse_y" keyword.

Step 1

Create a sprite by right-clicking the "Sprite" menu and choosing "create sprite." Use the built-in paint-shop tools to draw a simple shape that can represent the player. A circle, square, or anything else suitably simple--will do.

Step 2

Create an object with that sprite by right-clicking the "object" menu and choosing "create object." Name it "follower," and click the sprite list box to assign it the sprite you just created. Don't close the object settings window.

Step 3

Click "add event." Click "step" and choose "begin step."If you want the object to always be located in the same position as the mouse cursor, then drag the "Jump to Position" icon into the "Actions" box. It is the one that looks like an arrow drawn between two "X" marks. In the pop-up menu, write "mouse_x" as the "X position" and "mouse_y" as the "Y position." Ensure that the "relative" checkbox is not marked. If, on the other hand, you want the object to literally follow the cursor around the screen (so that it is always moving towards the cursor, but not necessarily in the same position as it), drag the "Move towards" icon into the Actions box. It is the one that looks like an arrow pointing to a little dot. Again, in the pop-up, ensure "relative" is unchecked and enter "mouse_x" for the "X position," and "mouse_y" for the "Y position." In addition, set the speed you would like the object to move at. This will require some experimentation on your part to get the desired effect, but "5" works well.

Step 4

Create a room by right-clicking "room" and choosing "create new room."

Step 5

Add the follower object to the room by clicking somewhere within.

Step 6

Start the game by clicking the green arrow at the top of the screen. Depending on which method you chose in Step 3, the follower object will either instantly teleport to the location of your mouse each time you move it, or it will begin to chase the mouse pointer around the screen.

References & Resources