New Input System: Triggers action on press and release

Hello!

I’ll try to be as succinct as possible.

  • Pressing an action key will trigger my code on press and upon release.
  • This happens when no interactions are set (default == press only)
  • This happens when explicitly configured to Press Only
  • I am using the Player Input component and OnAction(InputAction.CallbackContext context) functions (see example below).

So, if triggering this function would print “Attack” twice - once on press, and once on release.

    public void OnLightAttack(InputAction.CallbackContext context)
    {
        print("Attack");
    }

Any help would be greatly appreciated.

    public void OnLightAttack(InputAction.CallbackContext context)
    {
        if (context.performed)
            print("Attack");
    }

Been confusing to many users. A proper streamlining pass for PlayerInput (and in general) is coming.

1 Like

Ah! That actually clears it up quite a bit.

If I can take the opportunity to ask - what’s the advantage using UnityEvent + context (calls? I don’t know the verbiage) vs sending messages from the Player Input component to call methods?

UnityEvent

  • Much cheaper calls
  • More robust
  • Needs explicit setup
  • Invocation model is currently more complicated (need to deal with CallbackContext and phases and stuff)

Messages

  • Zero setup
  • Simpler invocation model (but some more complicated stuff can’t be done with it)
  • Slow
  • May end up calling methods not intended to be called

Streamling the UnityEvent path to have a simplified flow like that of messages is on the list.

this is quite similar so I have a duck script and when you put down then the player will duck and when it is released he will go back up so that is done with an if and if else how do I do that with this

This doesn’t take 2 years to add.

still not added?

Its completely weird to set up interactions as Press only, and then have your event fire multiple times lol then have to add context checks…

Still not added.

Yeah, and I doubt it will be added, @Rene-Damm hasn’t been online since the 9th of June, 2022. Is he even still working at Unity or not?!

This is still actively an issue in Unity 6.

He’s moved on…

image

Unfortunate.

Well if it is any help to others who come across this thread: While I wasn’t able to limit the event calling to only on press or release, I was able to determine what state the event was in.

For whatever reason the base objects event phase was useless, it was always in ‘processed’ (or something similar, it’s like midnight here when I’m writing this). However, the control object in the context was accurately reporting it’s button state.

So far the inputContext.control.IsPressed() has worked perfectly.

As a side-note if you don’t want your clicks to follow thru when you click over a UI element (uGUI), the line !EventSystem.current.IsPointerOverGameObject() works beautifully to terminate the event callback early. It won’t stop the action event from firing even though the UI was clicked as well, but this works great to cut it short. Also, it will log a warning into the console that it might be incorrect and shouldn’t be used in a callback context. But honestly it’s worked perfectly so far and better than any other solution I’ve come across.

Some people poll the IsPointerOverGameObject every Update to be more consistent, but I’m gonna swap to that method only when this breaks for me.