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