Search Unity

How would you handle a GetButtonDown Situaiton with the New Input System?

Discussion in 'New Input System' started by WizByteGames, Feb 10, 2019.

  1. WizByteGames

    WizByteGames

    Joined:
    Mar 28, 2013
    Posts:
    37
    Basically I want to know how do you check to see if a button is being held down durring a frame. Instead of just being pressed. I'm not sure how to check for this.
     
  2. WizByteGames

    WizByteGames

    Joined:
    Mar 28, 2013
    Posts:
    37
    I have figured out how to do what I needed done. I didn't realize you could have to actions binded to the same button. So all I had to do was add a binding for pressing a button and another for releasing the same button.
     
  3. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    616
    There's some other ways you can go about it.

    If all you're interested in is getting one call on press and one on release, hook into started and cancelled.

    Code (CSharp):
    1. myAction.started += /* button was pressed */;
    2. myAction.cancelled += /* button was released */;
    If you want to continuously be notified for as long as the button is held, check the "Continuous" box on the action and use performed and cancelled.

    Code (CSharp):
    1. myAction.performed += /* button was pressed or is held */;
    2. myAction.cancelled += /* button was released */;
     
    FullMe7alJacke7 likes this.
  4. Albarnie

    Albarnie

    Joined:
    Oct 24, 2015
    Posts:
    6
    Came across this thread, and this solution doesn't work for me. GetButtonDown would be true if it had been pressed in that frame, and false otherwise, and there's no simple and clean way to do this in the new system. this would work however if Started and cancelled got called when using the 'Press' interaction, but they don't for some reason.
    Performed no longer gets called when you release either so you can't use the
    ctx.ReadValue<float>()
    anymore either.

    Either I am doing something wrong here or this needs to be fixed.
     
  5. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    616
    Using "Press" and setting it to "Press and Release", you should see "performed" when the button is pressed and "cancelled" when the button is released. In most cases, though, the explicit "Press" interaction shouldn't be necessary. It gives control over press points and such but even without any interaction on a binding to a button, you should "performed" getting called when the button is pressed and "cancelled" when the button is released. At least that's the expected behavior. If that doesn't happen, there's probably a bug.
     
  6. gyl09

    gyl09

    Joined:
    Jul 21, 2017
    Posts:
    8
    Hi,
    I try to check the phase but in both cases (press/release) it returns "Performed".

    Then in debug mode I saw that the Button Control has the members "wasPressedThisFrame" and "wasReleasedThisFrame", but I have another issue which is that when:

    - "Continuous" is unchecked, the event is not send when holding so the flag "wasPressedThisFrame" is not updated

    - "Continuous" is checked, the behaviour is ... the same, events are only raised on press and release action. Is there a point I missed on continuous feature? Do I need to add another interaction? Hold?

    My action config:
    - 'Continuous" checked/unchecked according to the test case
    - Interaction: Press - Press and Release

    I could update the flag "manually" after a Time.deltaTime delay but it seems not the most efficient way to achieve it.
    Thanks for your help
     
  7. Luminoth

    Luminoth

    Joined:
    Jan 15, 2013
    Posts:
    20
    Hi, I just wanted to jump in on this thread as well with a note that Press and Release is 100% always giving "performed" for both events, making it quite difficult to differentiate between the two events. Looking at the code in Github seems to confirm this behavior:


    Code (CSharp):
    1.  
    2.                 case PressBehavior.PressAndRelease:
    3.                     if (m_WaitingForRelease)
    4.                     {
    5.                         if (!isActuated)
    6.                             context.PerformedAndGoBackToWaiting();
    7.                         // No support for continuous mode.
    8.  
    9.                         m_WaitingForRelease = isActuated;
    10.                     }
    11.                     else if (isActuated)
    12.                     {
    13.                         context.PerformedAndGoBackToWaiting();
    14.                         // No support for continuous mode.
    15.  
    16.                         m_WaitingForRelease = true;
    17.                     }
    18.                     break;
    19.  
    I've also noticed that quick taps seem to give only the press "performed" without the release "performed". Is that intentional, and if so, is there a correct way to resolve it?
     
  8. jeremies_unity

    jeremies_unity

    Joined:
    Oct 16, 2018
    Posts:
    26
    I also can't find a way to trigger a button release. Tried every solution mentionned here.

    EDIT: Finally figured it out. The release is also triggered in .performed, NOT .canceled.
     
    Last edited: Jul 8, 2019
    Sonorpearl likes this.