Unity 3D – Key Sequence Input – Basics – JavaScript
NOTA BENE: in tutti gli script seguenti la sequenza NON VERRA’ RESETTATA in caso di errore!
Attach this script to a Gameobject in the scene
(KeyCode.) Sequence of 2 Keys – Basic
KeySequenceInput.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | // Key Sequence Input Controller // Attach this script to a Gameobject in the scene private var firstDown : boolean = false ; // false at the beginning, I have no typed yet private var allDown : boolean = false ; // false at the beginning, I have no typed yet function Update() { // Key sequence input controller START ###################### if (Input.GetKeyDown(KeyCode.UpArrow)) { // highlight the button or play a sound firstDown = true ; //1. return true if you push first key } //2. if the first key is true if (firstDown) { if (Input.GetKeyDown(KeyCode.RightArrow)) { // highlight the button or play a sound allDown = true ; //3. return true if you push first and second key } } //4. if first and second key are true do someting... if (allDown) { Debug.Log ( "Sequence OK!" ); // Debug Code //Do Something... firstDown = false ; // reset variables allDown = false ; } // Key sequence input controller END ######################## } |
Play the game and type the keys: UpArrow – RightArrow
(KeyCode.) Sequence of 4 keys – Basic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | // Key Sequence Input Controller // Attach this script to a Gameobject in the scene private var firstDown : boolean = false ; // false at the beginning, I have no typed yet private var secondDown : boolean = false ; // false at the beginning, I have no typed yet private var thirdDown : boolean = false ; // false at the beginning, I have no typed yet private var allDown : boolean = false ; // false at the beginning, I have no typed yet function Update() { // Key sequence input controller START ###################### if (Input.GetKeyDown(KeyCode.UpArrow)) { // highlight the button or play a sound firstDown = true ; //1. return true if you push first key } //2. if the first key is true if (firstDown) { if (Input.GetKeyDown(KeyCode.RightArrow)) { // highlight the button or play a sound secondDown = true ; //3. return true if you push first and second key } } if (secondDown) { if (Input.GetKeyDown(KeyCode.DownArrow)) { // highlight the button or play a sound thirdDown = true ; //4. return true if you push first and second key and third } } if (thirdDown) { if (Input.GetKeyDown(KeyCode.LeftArrow)) { // highlight the button or play a sound allDown = true ; //5. return true if you push first and second key and third and fourth } } //4. if all sequence is true do someting... if (allDown) { Debug.Log ( "Sequence OK!" ); // Debug Code //Do Something... firstDown = false ; // reset variables secondDown = false ; thirdDown = false ; allDown = false ; } // Key sequence input controller END ######################## } |
Play the game and type the keys: UpArrow – RightArrow – DownArrow – LeftArrow
(KeyCode.) Sequence of 4 keys – Basic + CountDown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | // Key Sequence Input Controller // Attach this script to a Gameobject in the scene private var firstDown : boolean = false ; // false at the beginning, I have no typed yet private var secondDown : boolean = false ; // false at the beginning, I have no typed yet private var thirdDown : boolean = false ; // false at the beginning, I have no typed yet private var allDown : boolean = false ; // false at the beginning, I have no typed yet private var endTime : float; // countdown variable function Start() { endTime = Time.time + 3; // countdown variable: 3 seconds to type the right sequence! } function Update() { // COUNTDOWN START ######################### var timeLeft : int = endTime - Time.time; // We do not need negative time if (timeLeft < 0){ timeLeft = 0; firstDown = false ; // reset variables, You Fail! secondDown = false ; thirdDown = false ; allDown = false ; // Retry or Abort... } // COUNTDOWN END ########################### // Key sequence input controller START ###################### if (Input.GetKeyDown(KeyCode.UpArrow)) { // highlight the button or play a sound firstDown = true ; //1. return true if you push first key } //2. if the first key is true if (firstDown) { if (Input.GetKeyDown(KeyCode.RightArrow)) { // highlight the button or play a sound secondDown = true ; //3. return true if you push first and second key } } if (secondDown) { if (Input.GetKeyDown(KeyCode.DownArrow)) { // highlight the button or play a sound thirdDown = true ; //4. return true if you push first and second key and third } } if (thirdDown) { if (Input.GetKeyDown(KeyCode.LeftArrow)) { // highlight the button or play a sound allDown = true ; //5. return true if you push first and second key and third and fourth } } //4. if all sequence is true do someting... if (allDown) { Debug.Log ( "Sequence OK!" ); // Debug Code //Next Level... firstDown = false ; // reset variables secondDown = false ; thirdDown = false ; allDown = false ; } // Key sequence input controller END ######################## } |
(KeyCode.) Sequence of 4 keys – Basic + CountDown – Separate function()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | // Key Sequence Input Controller // Attach this script to a GameObject in the scene private var firstDown : boolean = false ; // false at the beginning, I have no typed yet private var secondDown : boolean = false ; // false at the beginning, I have no typed yet private var thirdDown : boolean = false ; // false at the beginning, I have no typed yet private var allDown : boolean = false ; // false at the beginning, I have no typed yet private var endTime : float; function Start() { endTime = Time.time + 3; // 3 seconds to type the right sequence! } // End Start() function Update() { KeySequenceControl(); // call the function to control the sequence } // End Update() // KeySequenceControl() START ################################################################## function KeySequenceControl(){ // COUNTDOWN START ######################### var timeLeft : int = endTime - Time.time; // We do not need negative time if (timeLeft < 0){ timeLeft = 0; firstDown = false ; // reset variables, You Fail! secondDown = false ; thirdDown = false ; allDown = false ; // Retry or Abort... } // COUNTDOWN END ########################### // Key sequence input controller START ###################### if (Input.GetKeyDown(KeyCode.UpArrow)) { // highlight the button or play a sound firstDown = true ; //1. return true if you push first key } //2. if the first key is true if (firstDown) { if (Input.GetKeyDown(KeyCode.RightArrow)) { // highlight the button or play a sound secondDown = true ; //3. return true if you push first and second key } } if (secondDown) { if (Input.GetKeyDown(KeyCode.DownArrow)) { // highlight the button or play a sound thirdDown = true ; //4. return true if you push first and second key and third } } if (thirdDown) { if (Input.GetKeyDown(KeyCode.LeftArrow)) { // highlight the button or play a sound allDown = true ; //5. return true if you push first and second key and third and fourth } } //4. if all sequence is true do someting... if (allDown) { Debug.Log ( "Sequence OK!" ); // Debug Code //Next Level... firstDown = false ; // reset variables secondDown = false ; thirdDown = false ; allDown = false ; } // Key sequence input controller END ######################## } // KeySequenceControl() END #################################################################### |
(Input Manager) Sequence of 4 keys – Basic + CountDown – Separate function()
Control Keys using Unity3D Input Manager, so end users can setup keys:
(Input.GetKeyDown(KeyCode.UpArrow) -> (Input.GetKeyDown(“up”)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | // Key Sequence Input Controller // Attach this script to a GameObject in the scene private var firstDown : boolean = false ; // false at the beginning, I have no typed yet private var secondDown : boolean = false ; // false at the beginning, I have no typed yet private var thirdDown : boolean = false ; // false at the beginning, I have no typed yet private var allDown : boolean = false ; // false at the beginning, I have no typed yet private var endTime : float; function Start() { endTime = Time.time + 3; // 3 seconds to type the right sequence! } // End Start() function Update() { KeySequenceControl(); // call the function to control the sequence } // End Update() // KeySequenceControl() START ################################################################## function KeySequenceControl(){ // COUNTDOWN START ######################### var timeLeft : int = endTime - Time.time; // We do not need negative time if (timeLeft < 0){ timeLeft = 0; firstDown = false ; // reset variables, You Fail! secondDown = false ; thirdDown = false ; allDown = false ; // Retry or Abort... } // COUNTDOWN END ########################### // Key sequence input controller START ###################### if (Input.GetKeyDown( "up" )) { // highlight the button or play a sound firstDown = true ; //1. return true if you push first key } //2. if the first key is true if (firstDown) { if (Input.GetKeyDown( "right" )) { // highlight the button or play a sound secondDown = true ; //3. return true if you push first and second key } } if (secondDown) { if (Input.GetKeyDown( "down" )) { // highlight the button or play a sound thirdDown = true ; //4. return true if you push first and second key and third } } if (thirdDown) { if (Input.GetKeyDown( "left" )) { // highlight the button or play a sound allDown = true ; //5. return true if you push first and second key and third and fourth } } //4. if all sequence is true do someting... if (allDown) { Debug.Log ( "Sequence OK!" ); // Debug Code //Next Level... firstDown = false ; // reset variables secondDown = false ; thirdDown = false ; allDown = false ; } // Key sequence input controller END ######################## } // KeySequenceControl() END #################################################################### |
(Input Manager) Sequence of 4 keys – Basic + CountDown – Separate sequence variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | // Key Sequence Input Controller // Attach this script to a GameObject in the scene // Right Sequence var firstKey : String = "up" ; var secondKey : String = "right" ; var thirdKey : String = "down" ; var fourthKey : String = "left" ; private var firstDown : boolean = false ; // false at the beginning, I have no typed yet private var secondDown : boolean = false ; // false at the beginning, I have no typed yet private var thirdDown : boolean = false ; // false at the beginning, I have no typed yet private var allDown : boolean = false ; // false at the beginning, I have no typed yet private var endTime : float; function Start() { endTime = Time.time + 3; // 3 seconds to type the right sequence! } // End Start() function Update() { KeySequenceControl(); // call the function to control the sequence } // End Update() // KeySequenceControl() START ################################################################## function KeySequenceControl(){ // COUNTDOWN START ######################### var timeLeft : int = endTime - Time.time; // We do not need negative time if (timeLeft < 0){ timeLeft = 0; firstDown = false ; // reset variables, You Fail! secondDown = false ; thirdDown = false ; allDown = false ; // Retry or Abort... } // COUNTDOWN END ########################### // Key sequence input controller START ###################### if (Input.GetKeyDown(firstKey)) { // highlight the button or play a sound firstDown = true ; //1. return true if you push first key } //2. if the first key is true if (firstDown) { if (Input.GetKeyDown(secondKey)) { // highlight the button or play a sound secondDown = true ; //3. return true if you push first and second key } } if (secondDown) { if (Input.GetKeyDown(thirdKey)) { // highlight the button or play a sound thirdDown = true ; //4. return true if you push first and second key and third } } if (thirdDown) { if (Input.GetKeyDown(fourthKey)) { // highlight the button or play a sound allDown = true ; //5. return true if you push first and second key and third and fourth } } //4. if all sequence is true do someting... if (allDown) { Debug.Log ( "Sequence OK!" ); // Debug Code //Next Level... firstDown = false ; // reset variables secondDown = false ; thirdDown = false ; allDown = false ; } // Key sequence input controller END ######################## } // KeySequenceControl() END #################################################################### |
(Input Manager) Sequence of 4 keys – Basic + CountDown – Arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | // Key Sequence Input Controller // Attach this script to a GameObject in the scene // array START ####################### var i : int = 0; // array index start value var sequenceKey = new Array (); // Right Sequence sequenceKey[0] = "up" ; sequenceKey[1] = "right" ; sequenceKey[2] = "down" ; sequenceKey[3] = "left" ; // array END ######################### private var firstDown : boolean = false ; // false at the beginning, I have no typed yet private var secondDown : boolean = false ; // false at the beginning, I have no typed yet private var thirdDown : boolean = false ; // false at the beginning, I have no typed yet private var allDown : boolean = false ; // false at the beginning, I have no typed yet private var endTime : float; function Start() { endTime = Time.time + 3; // 3 seconds to type the right sequence! } // End Start() function Update() { KeySequenceControl(); // call the function to control the sequence } // End Update() // KeySequenceControl() START ################################################################## function KeySequenceControl(){ // COUNTDOWN START ######################### var timeLeft : int = endTime - Time.time; // We do not need negative time if (timeLeft < 0){ timeLeft = 0; firstDown = false ; // reset variables, You Fail! secondDown = false ; thirdDown = false ; allDown = false ; // Retry or Abort... } // COUNTDOWN END ########################### // Key sequence input controller START ###################### if (Input.GetKeyDown(sequenceKey[0])) { // highlight the button or play a sound firstDown = true ; //1. return true if you push first key } //2. if the first key is true if (firstDown) { if (Input.GetKeyDown(sequenceKey[1])) { // highlight the button or play a sound secondDown = true ; //3. return true if you push first and second key } } if (secondDown) { if (Input.GetKeyDown(sequenceKey[2])) { // highlight the button or play a sound thirdDown = true ; //4. return true if you push first and second key and third } } if (thirdDown) { if (Input.GetKeyDown(sequenceKey[3])) { // highlight the button or play a sound allDown = true ; //5. return true if you push first and second key and third and fourth } } //4. if all sequence is true do someting... if (allDown) { Debug.Log ( "Sequence OK!" ); // Debug Code //Next Level... firstDown = false ; // reset variables secondDown = false ; thirdDown = false ; allDown = false ; } // Key sequence input controller END ######################## } // KeySequenceControl() END #################################################################### |