Categories: Unity, C #, VS2015
Date Created: 2016-04-21 I. INTRODUCTION
In the game of desktop system, mouse input is one of the most basic input methods. Many of the game's operations require a mouse to complete, such as the weapon's aim and fire, menu click, Object Pickup and so on.
Mouse input related events include mouse movement, button click, and so on.
The methods and variables related to mouse input in the input class are as follows:
In unity, the mouse position is represented by the pixel coordinates of the screen, the lower-left corner of the screen is the coordinate origin (0,0), the upper-right corner is (screen.width,screen.height), Where Screen.width is the width of the screen resolution, Screen.height is the height of the screen resolution.
The variable type of mouseposition is Vector3, where the X component corresponds to the horizontal coordinate, the Y component corresponds to the vertical coordinate, and the z component is always 0.
Cetmousebuttondown, Cetmousebuttonup, Cetmousebutton these 3 methods need to pass parameters to specify which mouse button, 0 for the left key, 1 for the right, and 2 for the middle key. Ii. Examples of basic usage
The following code shows how to respond to a mouse click event (0 corresponds to the left mouse button, 1 for the right mouse button, and 2 for the middle mouse button).
void Update ()
{
Press the left mouse button
if (Input.getmousebuttondown (0))
{
//...
}
Hold down the left mouse button
if (Input.getmousebutton (0))
{
//...
}
Lift the left mouse button
if (input.getmousebuttonup (0))
{
//...
}
Press the right mouse button
if (Input.getmousebuttondown (1))
{
//...
}
Hold down the right mouse button
if (Input.getmousebutton (1))
{
//...
}
Raise the right mouse button
if (Input.getmousebuttonup (1))
{
//...
}
}
Example (demo2_1_rotateexample.unity)
This example shows how to rotate the model based on the mouse movement distance to observe.
Add the following script (RotateExample.cs file) to the model you want to rotate, and the model rotates as the mouse moves:
usingUnityengine;usingSystem.Collections; Public classrotateexample:monobehaviour{ Public floatHorizontalspeed =6.0f; Public floatVerticalspeed =6.0f; voidUpdate () {floatH = horizontalspeed * Input.getaxis ("Mouse X"); floatv = verticalspeed * Input.getaxis ("Mouse Y"); Transform. Rotate (V, H,0); }}
Operating effect:
"Unity" 7.2 mouse input