Unity input and Control

Source: Internet
Author: User

Main concepts:

1. Virtual Axis

2. Enter pre-settings

Example:

Control object movement through keyboard

1 private float speed = 10.0f; // speed 2 3 private float rotaspeed = 100366f; // rotation speed 4 5 // update is called once per frame 6 void Update () {7 8 // obtain the virtual axis in the vertical direction and control W, S 9 float translation = input. getaxis ("vertical") * speed; 10 11 // obtain the virtual axis in the horizontal direction and control a, D12 float rotation = input. getaxis ("horizontal") * rotaspeed; 13 14 // variable by Time Difference 15 translation * = time. deltatime; 16 rotation * = time. deltatime; 17 18 // convert 19 transform. translate (0, 0, translation); 20 transform. rotate (0, rotation, 0); 21}

 

Note:

rotation *= Time.deltaTime;

To obtain the moving effect (smoothness) unrelated to the refresh frame rate)

 

3. keyboard Detection

Getkey and getbutton

Two getkey methods:

if (Input.GetKey(KeyCode.UpArrow))        {            print("get up");        }
if (Input.GetKey(“up”))        {            print("get up");        }

Getbutton method:

if(Input.GetButton("Fire1")){            print("pressed Fire1");        }

Getbutton obtains the Virtual Axis name.

Can be used to detect the occurrence of a single keyboard event, such as shooting

 

4. mouse events

Detection click event:

If (input. getmousebuttondown (0) {print ("Left click pressed ");}

0-left, 1-right, 2-middle

 

Long-pressed event detection:

If (input. getmousebutton (0) {print ("Left click pressed ");}

 

Release detection buttons:

If (input. getmousebuttonup (0) {print ("left button opened ");}

 

In the GUI system, double-click detection as follows

Void ongui () {event E = event. current; If (E. ismouse & (E. clickcount = 2) {debug. log ("double-clicked mouse ");}}

, Mainly using the GUI event mechanism.

 

OK.

Unity input and Control

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.