Time for action – reading mouse position in Unity
To begin, we'll create a GUI object that displays the x and y coordinates of our mouse as we move it. We'll be able to observe the range and current position of the mouse, which we can then use to write a gesture function. Perform the following steps to read mouse position:
Open the Create menu in the Hierarchy window and select GUI Text to create a new text object.
Your default GUI Text object will appear as shown in the following screenshot:
We don't want this text to obscure our game at all, so we'll reposition it to stay in the upper-left corner of the screen.
Click on the GUI Text object in the Hierarchy window to display it in the Inspector window.
Change the X and Y values of the GUI Text object's Transform component to
0
and1
, respectively.Change the color of the text in the object by selecting a new color from the Color field in the GUIText component.
After performing the previous two steps, your GUI Text object should appear as shown...