How to access mouse inputs in Unity
In most computer based video games, mouse input is necessary for movement and selection. Unity game engine makes this relatively simple for us to access mouse inputs.
Input module is what we will be using for Unity.
In the following examples, 0 stands for the left mouse button, 1 stands for the right mouse button and 2 stands for the middle/scroll wheel.
Input.GetMouseButtonDown(0);
Input.GetMouseButton(0);
Input.GetMouseButtonUp(0);
GetMouseButtonDown returns true when the user has pressed a mouse button.
GetMouseButton returns true when the user is holding down a mouse button.
GetMouseButtonUp returns true when the user is releasing a mouse button.
var xAxis = Input.GetAxis("Mouse X");
var yAxis = Input.GetAxis("Mouse Y");
In addition to clicking events, we have access to movement values from the mouse. The X and Y values will vary from -1 to 1.
X is -1 when moving left and 1 when moving right. Y is -1 when moving down and is 1 when moving up.