Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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:
    27
    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:
    27
    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:
    90
    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,155
    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,155
    Still not added.