Search Unity

How do I disable and enable the input system?

Discussion in 'Input System' started by Stephen1701, Aug 10, 2021.

  1. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    Hello All,

    I have the player input moving, he runs and jumps and does all sorts of things, however, I'd like to disable it and enable it at will, for example when I need to prevent player input while a big boss appears and shows off for a few seconds.

    I'm looking at this page on the unity docs which says to use PlayerInput.DeactivateInput. However I can't find a way to use this.

    Is it a case of using:
    PlayerInput input = GetComponent<PlayerInput>()
    then calling
    input.DeactivateInput()?
    The site doesn't say.
    The reason I can't try this at the moment is because my player input script is also called PlayerInput, and I don't know how to differentiate between the player input class I added to the gameobject, and the Unity PlayerInput that's added to the class to use the new input system.

    Thanks in advance.
     
  2. schwmm

    schwmm

    Joined:
    Oct 20, 2020
    Posts:
    62
    Hi,
    you probably want to deactivate the actionmap that is used for the player movement, i.e. by calling

    Code (CSharp):
    1. input.YourActionMap.Disable();
     
    Stephen1701 likes this.
  3. Pixelith

    Pixelith

    Joined:
    Jun 24, 2014
    Posts:
    576
    you can actually do the whole input.
    Code (CSharp):
    1. PlayerInput input = new PlayerInput();
    2. input.Enable();
    3. input.Disable();
     
    Stephen1701 likes this.
  4. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    Thank you. I'll give that a try.
     
  5. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    713
    Does this code work if you are using the Legacy input system?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,802
    Uh no. It's for the new input system. It's not relevant at all to the old system.
     
    rmele09 likes this.
  7. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    713
    Ok thanks. Any idea how to do it with the old system?
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,802
    You can't disable the old input system as far as I'm aware. You can only introduce your own system to guard any code that reads input from executing.
     
    rmele09 likes this.
  9. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    713
    Ya I was thinking of just making another bool around any input code, is that what you mean?
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,802
    Yes, that is what I mean.

    Though really I would use a guard-pattern:
    Code (CSharp):
    1. private void Update()
    2. {
    3.     if ([GameIsPaused] == true)
    4.     {
    5.         return;
    6.     }
    7.    
    8.     // input reading code
    9. }
     
    rmele09 likes this.
  11. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    713
    I'll have to research this, I've never used this pattern before! Thank you.