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
// 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
// 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
// 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()
// 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”)
// 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
// 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
// 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 ####################################################################