How to Make an Object Move Towards the Mouse in Game Maker | Step by Step Guidelines
When you are working with the game maker, you may want to make an object to interact with the cursor of your mouse. To make it more specific, you may want to move an object toward the mouse pointer automatically.
Fortunately, you’ve come to the accurate place where you will learn how to make an object move towards the mouse in the game maker. We will guide you on moving an object towards the mouse and provide the necessary steps.
So, what are you waiting for? Let’s get started.
How to Make an Object Move Towards the Mouse in Game Maker?
Making an object move toward the mouse pointer can be done easily on the game maker using a few built-in functions. Before that, you need to define the mouse pointer position.
The “X position” defines the distance between the pointer and the left side of the screen and the “Y position” defines the distance between the pointer and the top of the screen.
At first, retrieve the “X position” by using the “mouse_x” keyword and the “Y position” using the “mouse_y” keyword. Once you have successfully retrieved the mouse position, follow the steps below to make an object move towards the mouse cursor.
Step 1: Create a Sprite
First, you need to create a sprite by clicking on the “Sprite” menu and choosing “create sprite”. You can give any shape to the sprite using the built-in paint-shop tools that you want to represent the player. A circle, square, or any simple shape will do in this case.
Step 2: Create an Object
After creating a sprite, create an object using that sprite by right-clicking on the “object” menu and selecting “create object”. You can name it whatever you want and assign it to the sprite list box.
For example, if you created an object, and named it “Follower”, click the sprite list box and assign “follower” to it.
Note: Do not close the object setting window after this step.
Step 3: Make the Object Follow the Cursor
After the 2nd step, click “add event”. Then click “step” and choose “begin step”.
To make the object follow the cursor, drag the “Move towards” icon to the Action box. Then, write “mouse_x” in the X position and “mouse_y” in the Y position in the following pop-up menu. Do not mark the checkbox named Relative.
Step 4: Create a Room
To create a room, simply right-click on “room” and select “create a new room”.
Step 5: Add the Object to the Room
Click somewhere within the room and add the follower object to the room.
Step 6: Start the Game
After applying all the steps above, click on the Green Arrow at the top of the screen and the object will begin to follow the cursor of your mouse around the screen.
Bonus Tips: How Do I Make2D Object Rotate To The Mouse Pointer?
Here is the code to rotate the object towards the mouse pointer:
- var mouse_pos : Vector3;
- var target : Transform; //Assign to the object you want to rotate
- var object_pos : Vector3;
- var angle : float;
- function Update ()
- {
- mouse_pos = Input.mousePosition;
- mouse_pos.z = 5.23; //The distance between the camera and object
- object_pos = Camera.main.WorldToScreenPoint(target.position);
- mouse_pos.x = mouse_pos.x – object_pos.x;
- mouse_pos.y = mouse_pos.y – object_pos.y;
- angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
- transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
- }
Frequently Asked Questions
How Do You Make an Object Move Towards the Mouse in Unity?
If you are working with Utility and want to make an object move towards the mouse, then first you need to add the mouse pointer and create an object. After that apply the following code to make that object follow the mouse:
- public float moveSpeed = 10;
- void Update()
- {
- Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- transform.position = Vector2.MoveTowards(transform.position, mousePosition, moveSpeed * Time.deltaTime);
- }
How Do You Code Gravity in Game Maker?
Here is the code for gravity in game maker
- Syntax: gravity;
- Returns: Real (single precision floating point value)
- Back: Instance Variables.
- Example: if ! place_meeting(x, y + 1, obj_Ground) { gravity = 0.01; } else. { gravity = 0; }
Note: In the above code, the object will only find gravity if there are no instances of “obj_Ground” underneath it.
Conclusion
The game maker is a smart platform to develop games. So, you don’t need to face any hassle to make an object to follow the mouse pointer as it has many built-in functions for that. We hope, now you can make your object follow your cursor by following the given instructions from this article. If you have any other solutions, then please share them with us in the comment section below.
Subscribe to our newsletter
& plug into
the world of technology