How to get the mouse position in Unity
Unity Get Mouse Position
There are some instances where you need to use the position of your mouse to trigger an action in your game. For instance, you may want a game object to face the direction of the mouse and fire something. You will need to get your mouse position first before you can achieve something like that.
In this article, you will learn how to use both the Input Manager and the new Input System to get the position of a mouse.
📄 Resources:
- Top 9 tips for indie game devs
- Indie game programming for complete beginners
- How to market your indie game
How to use Unity’s Input Manager to get the mouse position
The mousePosition is a function of Unity’s Input manager. It returns the position of the mouse on the game window even when it’s not on the game window. When the position values are smaller than 0, this indicates that the mouse cursor is outside the game window. The function is a Vector3 with the z component always 0. Therefore, it can be used with functions that have Vector3 arguments.
To get the mouse position in Unity using Input.mousePosition
;
using UnityEngine;
public class MousePosition : MonoBehaviour
{
void Update()
{
Vector3 position = Input,mousePosition;
Debug.Log(position.x)
Debug.log(position.y)
}
}
In the example script above, the x and y coordinates of the mouse position are printed out to the console.
However, the mousePosition function just returns the screen coordinates. What if you want to do something with the mouse position in the game world? You’ll need to convert the screen coordinates to a real position in the game world.
How to convert the screen coordinates of the mouse position to the world coordinates
You can achieve this with the Camera.ScreenToWorldPoint
function.
Here’s a basic example;
using UnityEngine;
public class MousePosition : MonoBehaviour
{
void Update()
{
Vector3 mousePosScreen = Input.mouseposition;
Vector3 mousePosWorld = Camera.main.ScreenToWorldPoint(mousePosScreen);
Debug.Log(mousePosWorld)
}
}
In the example script above, the screen coordinate of the mouse position is gotten with the mousePosition function. Then the world coordinate is calculated by passing the screen coordinates as an argument to the ScreenToWorldPoint function. Then, the coordinate is printed to the console. You can add other functionalities instead of printing the coordinate to the console.
Instead of using the mousePosition function of the Input manager. You can use the new input system of Unity to get the position of a mouse.
How to use the new input system to get the mouse position
The first thing to do is to install the new input system from the Package Manager (if you haven’t). On the Package Manager, search for Input System, then install it.
A straightforward way to get the mouse position is to use the Mouse.current.position.ReadValue()
in the new input system in Unity.
Like this;
using UnityEngine;
using UnityEngine.InputSystem
public class MousePosition : MonoBehaviour
{
private void Update()
{
Vector2 mousePos = Mouse.current,position.ReadValue();
Debug.Log(mousePos)
}
}
In the example script above, the Mouse.current.position.ReadValue()
gets the position of the mouse cursor, and then returns the mouse position in screen coordinates. You can convert the screen coordinates to world coordinates with the ScreenToWorldPoint()
as explained earlier. You can use the coordinates as desired in your application.
Wrap Up
Now, you have learned how to get the position of a mouse cursor using both the Input Manager and the new Input System. It’s now up to you to leverage the knowledge you gained from this article in your next projects.
📄 Resources: