Search Unity

Question How to solve double functionality of a controler element with new input system?

Discussion in 'Input System' started by Hellhound_01, Dec 17, 2020.

  1. Hellhound_01

    Hellhound_01

    Joined:
    Mar 5, 2016
    Posts:
    102
    Any idea how I could solve a double functionality of a controler element? I.e. click to move and shift + click to run on same button? If I define two separated actions, both will always executed and I see no way to block an action if an other has been occured.



    Any hints on how to approach this?
     
    Last edited: Dec 17, 2020
  2. Holonet

    Holonet

    Joined:
    Aug 14, 2017
    Posts:
    84
    I'd suggest using the events. I'm not sure if this is by design, but when I try to use Unity Events, it triggers the method you wire up regardless of the Interaction, making the Interaction useless...but then you can control based on the phase. What you want can be done like this:

    Code (CSharp):
    1. private bool triggered = false;
    2.  
    3.     /// <summary>
    4.     /// Button you want held down
    5.     /// </summary>
    6.     /// <param name="context"></param>
    7.     public void OnTriggerButton(InputAction.CallbackContext context)
    8.     {
    9.         if (context.phase == InputActionPhase.Performed)
    10.             triggered = true;
    11.         else if (context.phase == InputActionPhase.Canceled)
    12.             triggered = false;
    13.     }
    14.  
    15.  
    16.     /// <summary>
    17.     /// The button that will trigger the action if the other is held down
    18.     /// </summary>
    19.     /// <param name="context"></param>
    20.     public void OnActionButton(InputAction.CallbackContext context)
    21.     {
    22.         if (context.phase == InputActionPhase.Performed && triggered)
    23.         {
    24.             // Perform the desired action
    25.         }
    26.     }
    The "cancelled" phase is when you release the button, so you can just flip the bool that way.