Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question What Is The Correct Way To Check If Any Button From An Action Map Was Pressed?

Discussion in 'Input System' started by Payaso_Prince, Sep 27, 2023.

  1. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    92
    Good Morning All,

    I am using Unity's New Input System. I have an Action Map created called 'Menu'.

    Currently, this is how I check for specific button presses.

    Code (CSharp):
    1. If( xxx.Menu.Select.WasPressedThisFrame() )
    2. {
    3.   // Do Stuff
    4. }
    The above works fine. However, now I need to check if any button was pressed from the Action Map.

    For example, my game could show an image of a PS4's X Button. I need to detect if the player presses the right or wrong button. If the right button is pressed they win and if the wrong button is pressed they lose.

    Hopefully this makes it even more clear:

    Code (CSharp):
    1. If( METHOD THAT CHECKS IF ANY INPUT FROM THE MENU ACTION MAP WAS PRESSED ) // <- This is what I need
    2. {
    3.      If( xxx.Menu.Select.WasPressedThisFrame() )
    4.      {
    5.        // You win!
    6.      }
    7.  
    8.      Else
    9.      {
    10.        // You Lose!
    11.      }
    12. }
    To summarize, I'm looking for a method from Unity's New Input System that checks if any button was pressed. Thank you so much for taking the time!
     
    Last edited: Sep 27, 2023
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,332
  3. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    92
    Hi, thanks for the response! :)
    I assume you're referring to the event "onAnyButtonPress", correct?

    If that's the case, I did see that, but unfortunately Event Callbacks don't fit well with what I'm doing. :(
    I was more so looking for a gamepad/button version of "Keyboard.current.anyKey.wasPressedThisFrame".


    I do really appreciate the reply all the same, though.:D
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,332
    Why doesn't it? Just subscribe to the delegate and check if the
    InputControl.device
    is from a gamepad.
     
  5. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    92
    Just wanted to close the loop on this!

    Here's what I ended up doing. I created an InputAction in my Action Map called "Any" with bindings for every gamepad button. That way, any button gets detected initially. I really hope this helps someone else out in the future!

    Code (CSharp):
    1. if (inputActions.actionMap.Any.WasPressedThisFrame())
    2. {
    3.    if (inputActions.actionMap.Good.WasPressedThisFrame())
    4.    {
    5.        Debug.Log("You win!");
    6.    }
    7.    else if (inputActions.actionMap.Bad.WasPressedThisFrame())
    8.    {
    9.        Debug.Log("You lose!");
    10.    }
    11. }