Search Unity

How to cancel either touch input or keyboard input

Discussion in 'Scripting' started by TGKG, Dec 2, 2015.

  1. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    This sounds way to simple a problem, however I cannot find an answer.
    I have a game and have noticed that while the scene is loading the player can touch or press the "Jump" key and when the game is fully loaded (ie displays on screen) the game is already playing.
    How do I clear any touches or keyboard input until the game appears on screen?
     
    Last edited: Jul 14, 2017
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't think you've given enough detail to answer. What is a "Jump" key? Is this a Button on the screen? Or a key/button input defined in the Input module? Show us exactly how you're detecting this whatever-it-is, and maybe we can help.
     
    Kiwasi likes this.
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    block input with a bool thatt is setted to true when scene is loaded. or some point you want.
     
    Kiwasi likes this.
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Don't enable the player input script until you are ready to start accepting input from the player.
     
    DRRosen3, LeftyRighty and Kiwasi like this.
  5. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    Thanks for the responses.
    Here is my code that allows keyboard and touch input. This code is attached to a game object that gets instantiated upon loading of the scene 6.
    Code (CSharp):
    1.  
    2. private Rigidbody rb;
    3. public bool ballInPlay;
    4. void Awake ()
    5.     {      
    6.         rb = GetComponent<Rigidbody>();  
    7.      }
    8.  
    9. void Update ()
    10.     {
    11.        #if (UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER)
    12.  
    13.         if (Input.GetButtonDown("Jump") && ballInPlay == false && GamePause.pauseGame == false)
    14.         {
    15.           ballInPlay = true;
    16.           rb.velocity = new Vector3(ballTempVel.x, Mathf.Abs(ballTempVel.y), 0);
    17.          }
    18.  
    19.       #elif (UNITY_IPHONE || UNITY_ANDROID)
    20.       if (Input.touchCount == 1 && ballInPlay == false && GamePause.pauseGame == false)
    21.         {
    22.             foreach(Touch touch in Input.touches)
    23.             {
    24.                 Ray ray = Camera.main.ScreenPointToRay(touch.position);
    25.                 RaycastHit hit;
    26.  
    27.                 if(Physics.Raycast(ray, out hit, 100) && hit.transform.name.Contains("Pad"))
    28.                 {                  
    29.                     if(touch.phase == TouchPhase.Ended)
    30.                     {
    31.                        ballInPlay = true;
    32.                        rb.velocity = new Vector3(ballTempVel.x, Mathf.Abs(ballTempVel.y), 0);
    33.                     }
    34.                 }
    35.             }
    36.         }
    37.  
    38.         #endif
    39. }


    1. Jump key is the spacebar and is accessed from the Input.GetButtonDown function
    2. How do I know from code that the scene has finished loading? Here is the OnLevelWasLoaded script I have tried. (Note: level 6 is the game play scene that I am loading).
    Code (CSharp):
    1.  
    2. void OnLevelWasLoaded(int level)
    3.     {
    4.       if (level == 6)
    5.         {
    6.            Input.ResetInputAxes();
    7.            levelIsFinishedLoading = true;
    8.         }
    9.     }
    10.  
    Input.ResetInputAxes() does not clear the keyboard input and did not work

    3. martinmr and Munchy2007. I have tried using a bool and tried to prevent player input until ready, however I have found no means to block keyboard input and prevent key presses from still passing thru. Please elaborate how you would do this
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The suggested bool solution

    Code (CSharp):
    1. bool isInputAllowed;
    2.  
    3. void Update(){
    4.     if (isInputAllowed){
    5.         // Do everything here.
    6.     }
    7. }
    For more sophisticated control place a layer of abstraction between the Input class and your game.
     
    nosrepsiht2002 likes this.
  7. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    Thank you Bored. I believe that I am already doing this without success. However, what do you suggest I use to change the "isInputAllowed" to true? How do I prevent a key press from flowing thru this bool (ie how do I clear the Keyboard buffer)?
     
  8. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    How do I not enable the player input script?
     
  9. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Untick it in the inspector or set enabled=false in Awake(), then when the game starts it will be disabled, and you can set it to enabled in your script at the appropriate time.

    This assumes that you have your player input functions in a self contained script that can be disabled without affecting any other functionality.
     
  10. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    So here is what I tried. I created a new script attached to the same gameObject called "EnableGameStart" and in its Awake I disabled my gamePlay "BallInGameScript" script. And then in its Update I check if the player has pressed the space bar (ie Jump).
    Code (CSharp):
    1.  
    2. private BallInGame BallInGameScript;      
    3. private bool gameIsEnabled;    //used after the game is enabled thus preventing this Update from running more than once
    4.  
    5.     void Awake()
    6.     {
    7.         BallInGameScript = GetComponent<BallInGame>();
    8.  
    9.         gameIsEnabled = false;
    10.     }
    11.  
    12.     void Update ()
    13.     {
    14.         if (Input.GetButtonDown("Jump") && gameIsEnabled == false)
    15.         {  
    16.             BallInGameScript.enabled = true;
    17.  
    18.             gameIsEnabled = true;
    19.         }
    20.     }
    21.  
    If the player has pressed the spacebar then I enable the "BallInGameScript" script.
    However what I found is that any keyboard button presses made prior to enabling the "BallInGameScript" script are still in the keyboard buffer and once I enable the "BallInGameScript" script the keyboard button presses then get processed by the Input.GetButtonDown function and the game begins.

    What I really need is a way to empty the keyboard buffer. Any ideas?
     
  11. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    None at all sorry, I've never encountered this and I didn't think the input class stored key presses in a buffer.

    Maybe someone else can throw some light on it.
     
    Kiwasi likes this.
  12. jayalter

    jayalter

    Joined:
    Nov 21, 2014
    Posts:
    6
    I'm having a similar issue. I need to disable the "W, A, S, D" keys after the player has used up his character's alloted move points (which are ticked off after the character moves so many units of distance). I am able to find and receive the input events. I can stop the character in space by using the ResetInputAxes but it still allows the player to rotate the character. I've tried capturing the Keyboard events during OnGUI and then call Event.Use() for input of W, A, S and D but the player can still move the character. I know there has got to be some way to completely stop a player from moving a given object using the keyboard during a game. Anyone familiar with Mordheim - City of the Damned may recognize this is done in that game (which was developed using Unity). They have figured out how to freeze any given character from moving (or rotating) meanwhile allowing the camera to be rotated around the object. Has anyone had any success in this endeavor? I'm guessing the Event.Use() function needs to be called early so it superceeds any other functions from processing the event. While I can't be sure I think what is happening is other aspects of my setup are processing the input before my script has it's OnGui() function called. (BTW, I've tried this in FixedUpdate() as well to no avail).
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Consuming GUI Events will not help you. These are unrelated to input processing.

    Just throw in an isFrozen bool in your character controller. Then before moving anything check the bool. And if the bool is true don't move.
     
    jayalter likes this.
  14. jayalter

    jayalter

    Joined:
    Nov 21, 2014
    Posts:
    6
    Thanks for the imput BoredMormon! A great help! Much appreciated!
     
  15. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    So, still having this issue of keyboard input persisting. I could use a bool to solve this, if I knew when the level is finished displaying to the screen, "OnLevelWasLoaded" does not seem to be good enough.
    "OnLevelWasLoaded" becomes true prior to the level actually being loaded. I have verified this since I have music that changes when the Level is loaded and the music will change up to 10 seconds prior to the level showing up on screen. Therefore is there a solid method for knowing when the Level is finished loading AND is finished displaying to the screen??