"Unity" 7.2 mouse input

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.