Mouse and Cursor Inputs
📦 Unity packages from today's class:
Before importing both packages, make sure to install the Input System package on your Editor as well.
Legacy Input System
Directional Input from Mouse
You can use the axes Mouse X and Mouse Y to get information about how the mouse is moving.
Note that this does NOT give you the position of the cursor on screen, but tells you the direction and speed the player is moving the mouse.
This is often used to aim the camera in first person games.
float sensitivity = 8f;
public GameObject camera;
void Update()
{
float mouseX = Input.GetAxis("Mouse X");
camera.transform.Rotate(Vector3.up, mouseX * sensitivity * Time.deltaTime);
}
ScreenToWorldPoint and Raycasts with Cursor Input
Screen Space and World Space
It's easy enough to read the screen coordinates of the cursor.
Use Input.mousePosition to get the current position of the cursor in screen space.
Vector3 mousePos = Input.mousePosition;
//x is the x coordinate of the cursor in screenspace
//y is the y coordinate of the cursor in screenspace
//z is always 0.
In screen space, the bottom-left of the screen or window is at (0, 0). The top-right of the screen or window is the height and width of the screen in pixels, at (Screen.width, Screen.height).
The trick is to convert from screen space to the game's world coordinates (aka "world space").
How a position in screen space corresponds to world space depends on where the camera is looking, whether the camera uses perspective or orthographic projection, and how "deep" into the scene you want the cursor to be.
If you get a reference to the camera in your scene, you can convert from a screen coordinate to a world coordinate with Camera.ScreenToWorldPoint.
Camera camera;
void Start()
{
camera = GetComponent<Camera>();
}
void Update()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 1; //the z component of the vector will
//determine the distance of our new position from the camera
Vector3 worldPos = camera.ScreenToWorldPoint(mousePos);
}
In this case, ScreenToWorldPoint gives us a point one unit in front of the camera, with the same apparent position on screen as the cursor.
Raycasting from the camera
If you want to interact with objects in a 3D scene using the mouse, at some point you will have to use raycasting.
First, you need to get a Ray that passes from the camera, through your cursor, into the scene.
A Ray contains two vectors, which represent the ray's origin point in space and its direction vector.
Camera camera;
void Start()
{
camera = GetComponent<Camera>();
}
void Update()
{
Vector3 mousePos = Input.mousePosition;
Ray cursorRay = camera.ScreenPointToRay(mousePos);
}
Then, you can use the ray to raycast against the ground or other objects in your scene.
RaycastHit hitInfo;
if(Physics.Raycast(cursorRay, out hitInfo))
{
Debug.Log(hitInfo.point); //prints the position in the scene that the raycast hit...
//or do something else, depending on what you hit!
if (hitInfo.collider.CompareTag("Enemy")){
Debug.Log("Found Enemy!");
}
}