[New UnityInputSystem]: Fix input event detection for ButtonUp

echtnice

Member
Hi,

I am using the same code as in Jump (shown below) to detect if a user released a button but it does not work with the new input system:

Code:
                if (m_Handler != null && InputIndex != -1) {
                    m_HoldInput = GenericObjectPool.Get<ActiveInputEvent>();
                    m_HoldInput.Initialize(ActiveInputEvent.Type.ButtonUp, InputNames[InputIndex], "OnNgSlideAbilityReleaseHold");
                    m_Handler.RegisterInputEvent(m_HoldInput);
                }
                EventHandler.RegisterEvent(m_GameObject, "OnNgSlideAbilityReleaseHold", OnReleaseHold);

Changing UnityInputSystem.GetButtonUpInternal to the following code fixed the problem for me. It seems that if the button was released it is not set as action.activeControl anymore. Iterating over action.controls will find the button and has wasReleasedThisFrame set though.

Code:
        protected override bool GetButtonUpInternal(string name)
        {
            var action = m_PlayerInput.currentActionMap.FindAction(name);
            if (action != null) {
                for (int i = 0; i < action.controls.Count; i++) {
                    if (action.controls[i] is ButtonControl button && button.wasReleasedThisFrame) {
                        return true;
                    }
                }
            }
            return false;
        }

Thanks!
 
Top