Version that does not allow new input until key is physically released

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NumberWizard : MonoBehaviour
{
    bool userinputactive;
    KeyCode keycode;


    // Start is called before the first frame update
    void Start()
    {
        int max = 1000;
        int min = 1;
        userinputactive = false;
        Debug.Log("Welcome to Louis Number Wizard");
        Debug.Log("I want you to think of a number...");
        Debug.Log("No higher than "  + max);
        Debug.Log("No lower than " + min);
        Debug.Log("Tell me if your number is higher or lower than 500");
        Debug.Log("Up Arrow = Higher, Down Arrow = Lower, Enter = Correct");


    }

    // Update is called once per frame
    void Update()
    {
        //Detect when the up arrow key is pressed down
        if(!userinputactive)
        {
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                Debug.Log("Up Arrow key was pressed.");
                keycode = KeyCode.UpArrow;
                userinputactive = true;
            }
            if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                Debug.Log("Down Arrow key was pressed.");
                keycode = KeyCode.DownArrow;
                userinputactive = true;
            }
            if (Input.GetKeyDown(KeyCode.Return))
            {
                Debug.Log("Enter key was pressed.");
                keycode = KeyCode.Return;
                userinputactive = true;
            }            
        } else
        {
            if (Input.GetKeyUp(keycode))
            {
                userinputactive = false;
            }
        }
        

    }
}
1 Like

Incredible code! :fire:

Privacy & Terms