[Unity 3D] Forty-six learning notes: input and control-Keyboard Events

Source: Internet
Author: User

In the game, players control the movement of the main character, attack keys, and choose to walk. You must listen to player input in the program. Unity provides developers with an input library to support Keyboard Events, mouse events, and touch events. This article mainly reviews Keyboard Events and reviews mouse and touch events in the future.



Keyboard Events

Generally, a PC keyboard has 104 different buttons. in the program, you can listen to these button events to further perform logical operations. For example, in a shooting game, W indicates forward, s indicates backward, A indicates left shift, and D indicates right shift.



Press event

In the script, input is used. The getkeydown () method uses the key value as a parameter to monitor whether the key is pressed. Press to return true. Otherwise, false is returned.

Using unityengine; using system. collections; public class script_07_01: monobehaviour {void Update () {If (input. getkeydown (keycode. w) {debug. log ("W key pressed");} If (input. getkeydown (keycode. s) {debug. log ("you pressed the s key");} If (input. getkeydown (keycode. a) {debug. log ("You pressed key a");} If (input. getkeydown (keycode. d) {debug. log ("you pressed the d key");} If (input. getkeydown (keycode. space) {debug. log ("you pressed the Space key ");}}}

Run:




Lift events

The lift event is totally dependent on the push event, because only the push event is lifted. We use the input. getkeyup () method to listen for the lift event. After the key is lifted, true is returned; otherwise, false is returned.

Using unityengine; using system. collections; public class script_07_02: monobehaviour {void Update () {// press the event if (input. getkeydown (keycode. w) {debug. log ("W key pressed");} If (input. getkeydown (keycode. s) {debug. log ("you pressed the s key");} If (input. getkeydown (keycode. a) {debug. log ("You pressed key a");} If (input. getkeydown (keycode. d) {debug. log ("you pressed the d key");} If (input. getkeydown (keycode. space) {debug. log ("you pressed the Space key");} // lift the key if (input. getkeyup (keycode. w) {debug. log ("You raised the W key");} If (input. getkeyup (keycode. s) {debug. log ("You raised the s key");} If (input. getkeyup (keycode. a) {debug. log ("You raised key a");} If (input. getkeyup (keycode. d) {debug. log ("You raised the d key");} If (input. getkeyup (keycode. space) {debug. log ("You have lifted the Space key ");}}}

Run:



Long press eventA long press event is used to monitor whether a key is always pressed. You can use input. getkey () to determine whether a key is always pressed on the keyboard.
Using unityengine; using system. collections; public class script_07_03: monobehaviour {// record the number of frames pressed by a button int keyframe = 0; void Update () {If (input. getkeydown (keycode. a) {debug. log ("A press once");} If (input. getkey (keycode. a) {// record the number of frames pressed. keyframe ++; debug. log ("A record:" + keyframe + "frame");} If (input. getkeyup (keycode. a) {// clear the number of frames after lifting keyframe = 0; debug. log ("A key lifted ");}}}

Run:





Any key eventIn the program, you can also monitor whether any button in the key is pressed, usually after loading the game, press any key to enter.
Using unityengine; using system. collections; public class script_07_04: monobehaviour {// record the number of frames pressed by a button. Int keyframe = 0; // update is called once per framevoid Update () {If (input. anykeydown) {// clear the number of pressed frames. keyframe = 0; debug. log ("any key is pressed");} If (input. anykey) {keyframe ++; debug. log ("any key is long pressed" + keyframe + "frame ");}}}
Run:



Instance-combination buttons

In a classic fighting game, there will be a combination of keys to make a great move, and the event thinking of this function is actually not difficult: After a player presses a key, it starts to count the time, in a certain period of time, press the required key to make a big move.
Using unityengine; using system. collections. generic; using system; public class script_07_05: monobehaviour {// texture public texture imageup on the arrow key; // texture public texture imagedown under the arrow key; // public texture imageleft; // public texture imageright; // storage value of the custom direction key: Public const int key_up = 0; Public const int key_down = 1; Public const int key_left = 2; Public const int key _ Right = 3; Public const int key_firt = 4; // The Event restriction for consecutive buttons public const int frame_count = 100; // Number of stored skills in the warehouse public const int sample_size = 3; // number of keys for each set of skills public const int sample_count = 5; // skill warehouse int [,] sample = {// lower + front + fist {key_down, key_right, key_down, key_right, key_firt}, // lower + front + lower + back + fist {key_down, key_right, key_down, key_left, key_firt}, // lower + back + fist {key_down, key_left, key_down, key_left, key_firt },}; // Record the key value int currentkeycode = 0 currently pressed; // indicates whether to enable the listening button bool startframe = false; // record the current time when the listener is enabled int currentframe = 0; // Save the list of key combinations entered by players for a period of time <int> playersample; // flag completed bool issuccess = false; void start () {// initial call key combination linked list playersample = new list <int> ();} void ongui () {// obtain the number of keys stored in the key combination Link Table. Int size = playersample. count; // traverse this key combination linked list for (INT I = 0; I <size; I ++) {// display the image corresponding to pressing the button in the screen int key = playersample [I]; texture temp = Null; Switch (key) {Case key_up: temp = imageup; break; Case key_down: temp = imagedown; break; Case key_left: temp = imageleft; break; Case key_right: temp = imageright; break;} If (temp! = NULL) {guilayout. label (temp) ;}}if (issuccess) {// The successful Paster guilayout is displayed. label (imagesuccess);} // The default message guilayout. label ("Continuous Combination of buttons 1: Bottom, front, bottom, front, and fist"); guilayout. label ("Continuous Combination of Buttons 2: Bottom, front, bottom, back, punch"); guilayout. label ("Continuous Combination of Buttons 2: Bottom, back, bottom, back, and punch");} void Update () {// update the updatekey (); If (input. anykeydown) {If (issuccess) {// reset issuccess = false; reset ();} If (! Startframe) {// startframe = true;} // Add the key value to playersample in the linked list. add (currentkeycode); // The Int size = playersample. count; If (size = sample_count) {for (INT I = 0; I <sample_size; I ++) {int successcount = 0; For (Int J = 0; j <sample_count; j ++) {int temp = playersample [J]; If (temp = sample [I, j]) {successcount ++ ;}} // The combination of the combined keys pressed by the player and the keys in the warehouse is the same, indicating that the skill is successfully released if (successcount = sample_count) {issuccess = true; break ;}}} if (St Artframe) {// counter ++ currentframe ++;} If (currentframe> = frame_count) {// counter timeout if (! Issuccess) {reset () ;}} void reset () {// reset key information currentframe = 0; startframe = false; playersample. clear ();} void updatekey () {// obtain the key information of the Current keyboard if (input. getkeydown (keycode. w) {currentkeycode = key_up;} If (input. getkeydown (keycode. s) {currentkeycode = key_down;} If (input. getkeydown (keycode. a) {currentkeycode = key_left;} If (input. getkeydown (keycode. d) {currentkeycode = key_right;} If (input. getkeydown (keycode. space) {currentkeycode = key_firt ;}}}
Press S, D, S, D, space:







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.