Search Unity

temporarily disable all input while camera animation plays??

Discussion in 'Scripting' started by Jeepster, Jan 17, 2019.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    In my game menu, when you choose your player by pressing enter or mouse click, everything lights up and some fx play for 1 second before the camera moves over to another part of my 3D menu. During that second, I need all input (Mouse and Keyboard) to be unresponsive, so as to not activate player rotation and fx again.

    There's only specific examples in relation to rigidbodies and movement that I could find. Can anyone tell me how to disable all input in a coroutine?

    Thank you in advance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You can set and check a boolean and explicitly ignore the events coming back from each UI element in your input handler. This is a good argument for centralizing your input processing for a given screen.

    Or you can put up a solid transparent object to "eat" all the touches, but then keyboard input also works still.

    Or you might also be able to grab the EventSystem object in the scene and turn it off (disable it) but I'm not sure if that might have other side effects.
     
    xeniaosense and Jeepster like this.
  3. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Thanks for your reply. I found this code that describes what you mention using a boolean to ignore input, but it doesn't work. I assume it's because the boolean doesn't reference input? but I'm not sure. Here's the code:
    Code (CSharp):
    1. private bool disableG; // this will be set to true or false
    2. private void Start()
    3. {
    4.      disableG = false;// we start it at false
    5. }
    6. private void Update () {
    7.      if (Input.anyKeyDown) // if you press down any key
    8.      {
    9.          if (!Input.GetKeyDown(KeyCode.G)) //and that key isn't g
    10.          {
    11.              disableG = true; //disable g is set to true
    12.          }
    13.      }
    14.      if (!Input.anyKey) //if you're not pressing anything
    15.      {
    16.          disableG = false; //disable g is set to true, this will ensure if you press and release a button you'll be able to press g again even if it's in a single fram
    17.      }
    18.      if (Input.GetKeyDown(KeyCode.G) && disableG == false) //now we see if you're pressing g and it hasn't been disabled by pressing another button
    19.      {
    20.          //do stuff
    21.      }
    22. }
     
  4. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I would abstract my input to a command strategy. That way its easy to disable input globally. Also you might not want to disable all commands, so maybe implement CommandGroups. Each group can be assigned a prio. Then you can call Disable(prio: 2) and all commands with 2 or lower prio will be disabled. For example.
     
  5. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    How do I abstract my input to a command strategy exactly?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Here's a super-simple summary of it. This is NOT abstracted:

    Code (csharp):
    1. if (Input.GetAxis("Jump") > 0.5f)
    2. {
    3.   // ... do all the jump logic
    4. }
    5. if (Input.GetKeyDown( Keycode.Space))
    6. {
    7.   // do all the jump logic... again!
    8. }
    Instead, this abstracts HOW you jumped from the fact that you jumped.

    The "ReadAllInputThatCanJump();" function reads keyboard, joystick, mouse, whatever, and sets the jumpCommanded boolean if commanded.

    Code (csharp):
    1. bool jumpCommanded;
    2.  
    3. void Update()
    4. {
    5.  jumpCommanded = false;
    6.  
    7.  ReadAllInputThatCanJump();
    8.  
    9.  // now at this point all the possible ways you can jump are reflected in the jumpCommanded boolean
    10.  
    11.  // now if you are paused, set jumpCommanded = false (referring back to your original question)
    12.  
    13.  if (jumpCommanded)
    14.  {
    15.   // .. do the jump logic
    16.  }
    17. }
     
    Jeepster likes this.
  7. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi Kurt, Thank you for explaining. I just don't understand how 'ReadAllInputThatCanJump();' gets to be "now at this point all the possible ways you can jump are reflected in the jumpCommanded boolean". Where does the link between the two happen?
    The script I uploaded before doesn't work, and it goes by the same logic as the example you're explaining. I'm sure I'm asking stupid questions, but I guess I just don't understand it.

    The idea that the boolean, any made up boolean, can be set to true or false and that boolean can be linked to specific input in the input manager makes total sense. It's where, in all this code, does the boolean link to the actual input manager? That's where I'm confused. And again, the code I submitted doesn't work, so the boolean "disableG" isn't referring to anything in the input manager ...
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Sorry, I wasn't clear about the above: in my example above, you can actually take the first snippet of code (the two "if" statements) and put them inside the ReadAllInputThatCanJump() function, and for the predicates (the part they do if they are true), set the boolean to true!

    Then later if you find a third thing that you want to make jump (such as "shake the phone violently") then you can add it in that one place, and all other behavior "downstream" of that boolean applies, such as when you are paused, etc.
     
    Jeepster likes this.
  9. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Ah perfect. Now I'm with you :) Thank you for explaining.