Search Unity

New Input System: Triggers action on press and release

Discussion in 'Input System' started by DrewofDonuts, Aug 26, 2021.

  1. DrewofDonuts

    DrewofDonuts

    Joined:
    Apr 23, 2020
    Posts:
    28
    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.

    Code (CSharp):
    1.     public void OnLightAttack(InputAction.CallbackContext context)
    2.     {
    3.         print("Attack");
    4.     }

    Any help would be greatly appreciated.
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Code (CSharp):
    1.     public void OnLightAttack(InputAction.CallbackContext context)
    2.     {
    3.         if (context.performed)
    4.             print("Attack");
    5.     }
    Been confusing to many users. A proper streamlining pass for PlayerInput (and in general) is coming.
     
  3. DrewofDonuts

    DrewofDonuts

    Joined:
    Apr 23, 2020
    Posts:
    28
    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?
     
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    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.
     
  5. NintendoAsh12

    NintendoAsh12

    Joined:
    Feb 15, 2021
    Posts:
    91
    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
     
  6. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,365
    This doesn't take 2 years to add.
     
  7. Milionario

    Milionario

    Joined:
    Feb 21, 2014
    Posts:
    138
    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...
     
    Last edited: Nov 14, 2023
  8. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,365
    Still not added.
     
  9. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    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?!