Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to properly use "Press And Release"?

Discussion in 'Input System' started by Kiloku, Nov 24, 2019.

  1. Kiloku

    Kiloku

    Joined:
    Mar 20, 2015
    Posts:
    4
    I have a button action called "Fire", bound to the Right Trigger.
    I added a "Press" interaction to the Right Trigger binding under it, and chose "Press and Release" as the behavior.
    upload_2019-11-24_19-48-24.png

    Then, based on the instructions in the tooltip, I set up my code like this:
    upload_2019-11-24_19-50-9.png

    My idea being that when I release the trigger, it should call the StopFiring() method.
    However, the "StopFiring" method seems to not be called at all, as the "Firing" boolean stays true and the Debug.Log is never printed to the screen.
     
  2. Forberg

    Forberg

    Joined:
    Oct 27, 2018
    Posts:
    23
    Got the same issue, this doesn't behave as documented!

    What it should do according to the doc/tooltip:
    Perform action on button press, Cancel action on button release

    What it does:
    Perform action on button press, Perform action on button release

    Using this to toggle a bool is fine for now, but I'm wondering if that's intended behaviour.
     
    Last edited: Dec 3, 2019
  3. Elledivar

    Elledivar

    Joined:
    Oct 25, 2017
    Posts:
    6
    upload_2019-12-4_23-23-20.png
    I too am looking forward to this feature of Unity, but somehow, I couldn't get it to work either.. This is what I do temporarily though... I created 2 Actions solely for a ButtonDown and a ButtonUp, I hope this gets resolved soon as well
     
  4. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    In the case above you can just inspect the phase of the action. Performed will be down and canceled will be up in a default button.
     
    nicholasroberts likes this.
  5. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    You can simply watch for context.ReadValue<float> (1 is pressed, 0 is released)
     
  6. Elledivar

    Elledivar

    Joined:
    Oct 25, 2017
    Posts:
    6
    AFAIK, the ReadValue gets called only when the button is pressed (and when the button is released if that was enabled in the setting)... Now I think what we want is something like a charge function, where the ButtonDown would initiate the charge and the ButtonUp releases the charge... The problem is that wouldn't some god-forsaken lag cause the controls to reverse their functions? Like once you initiate a charge, a lag spike occurs, as that happens you release the button, wouldn't there be a chance that the ButtonUp event won't register? If that happens, wouldn't the next ButtonDown release the charge, and the next ButtonUp initiate the charge? This scenario would lead us wanting ButtonDown to have an exclusive function (initiate charge), as well as for ButtonUp (release the charge).

    upload_2019-12-5_23-45-12.png
    I'm not sure if I'm doing this right since I dove into it just recently, but I've never had the ReadValue go to anything other than "0"... I tried it both on Update and FixedUpdate but it never registered as "1"... Same goes for the phase, it just states "Waiting"... Canceled doesn't seem to work either, from what I understand it only works with something like using Hold, and triggers when released prematurely... It shouldn't work on Press since the press occurs once and immediately, and doesn't have a fail-check?

    Closest thing I can think of is to use Hold, set the Hold Time to "infinite" and wait for the "canceled" event to occur...

    I'm probably missing a point somewhere...

    EDIT: Just found out that having absolutely NO INTERACTIONS in the settings solved the problem of not detecting the "canceled" event...:confused:
     
    Last edited: Dec 6, 2019
  7. DevBaro

    DevBaro

    Joined:
    Apr 23, 2019
    Posts:
    13
    I confirm that the solution to use the Controller Triggers as buttons the way is to left blank the "INTERACTIONS" section.

    But instead of using the "performed" event (which is called multiple times) it is necessary to use "started" and "canceled".

    Code (CSharp):
    1.  
    2.         InputGame.Game.Switch.started += ctx => EventFunction_TriggerRight(true);
    3.         InputGame.Game.Switch.canceled += ctx => EventFunction_TriggerRight(false);
    4.  
     
    Shawn_Flanagan likes this.
  8. rogvc

    rogvc

    Joined:
    Dec 13, 2018
    Posts:
    1
    DevBaro & Elledivar, y'all saved my life! I've spent hours today trying to figure this out (is this just a bug?) and clearing the Interactions just did it for me. I hope they fix/clarify this later in the documentation, because it sure is a lot more complicated than just using Input.GetButton (although in the long run it saves you a ton of time).
     
  9. Happy_Face

    Happy_Face

    Joined:
    Jul 18, 2017
    Posts:
    3
    Just an update for a simpler solution:


    Code (CSharp):
    1. void Awake() {
    2.     fireAction.performed += OnFire;
    3.     fireAction.canceled += OnFire;
    4. }
    5.  
    6. void OnFire(InputAction.CallbackContext context) {
    7.     firing = context.performed;
    8. }