InputSystem icon indicating copy to clipboard operation
InputSystem copied to clipboard

Binding an action to a button control detects button press twice for each single press

Open joelpryde opened this issue 8 years ago • 2 comments

Repro:

  1. Bind an action to a button control (in my case, this happened with Oculus and OpenVR A/B Buttons).
  2. In the action query if the button was just pressed.
void OnButtonAction(InputAction.CallbackContext context)
        {
            var buttonControl = context.control as ButtonControl;
            if (buttonControl != null && buttonControl.wasJustPressed)
            {
              // this code is reached twice for each button press
            }
        }

Expected: Single press detected in action when polling wasJustPressed.

joelpryde avatar Jan 05 '18 21:01 joelpryde

@joelpryde Could you show me the setup code where you install OnButtonAction? Is it bound to just the .performed callback? Does it behave differently if you put a Press modifier on the binding?

Also, is the button coming in a purely 0 and 1 or is it an analog button?

Buttons behave in unexpected ways ATM. Not yet sure how to best solve this. Actions that don't have any modifiers on them will simply trigger on every single value change. They have no concept of buttons vs axes. So if you bind an action to a trigger on a gamepad, for example, the action will trigger over and over instead of once per press. The "Press" action modifier solves that but it's not intuitive that you have to put it on the binding.

This is further made confusing by how wasJustPressed works. It compares the current value to the value before the last buffer swaps. The buffer swaps happen in sync with Update() and FixedUpdate(), but actions can process multiple value changes in a single Update() and FixedUpdate() frame. Thus wasJustPressed isn't comparing the value you'd expect it to.

In your case, you probably want to have a "Press" modifier on the binding and get rid of the if check entirely.

Still, not good this isn't all simple and obvious.

Rene-Damm avatar Jan 10 '18 15:01 Rene-Damm

No repro for this bug for a long time. It should be fixed in the past updates.

JiaUnity avatar Mar 12 '19 01:03 JiaUnity