Search Unity

PlayerInput - using scroll Up/Down as a button

Discussion in 'Input System' started by Lad-Ty, Jul 21, 2021.

  1. Lad-Ty

    Lad-Ty

    Joined:
    May 7, 2013
    Posts:
    61
    Hi, I'm trying to use the new input system (for me it's still new anyway).

    I am currently trying to support the possibility of switching between weapons - prev/next weapon. Switching using gamepad shoulders for example is awesome, or using keys. But I would also like to support the ability to switch using mouse scroll wheel in the same way as I do with the buttons.

    Is there a way to do that - for the wheel to be under the same action as the gamepad shoulders for example, using some modifiers and whatnot? I must admit, I am quite lost with all these. But very much hope I won't have to deal with detecting it as a 1D axis instead and so on?
     
  2. ZingZangGames

    ZingZangGames

    Joined:
    Dec 11, 2020
    Posts:
    73
    Just do the following (but you need to create a new InputAction with type Button):

    Code (CSharp):
    1. public InputAction scrollWithButtonToNext;
    2. public InputAction scrollWithButtonToLast;
    3. public Scrollbar scrollBar;
    4. public float changeValue = 0.1f;
    5.  
    6. void OnEnable()
    7. {
    8.    // If we press the button to scroll for the next item
    9.    scrollWithButtonToNext.performed += ChangeToNext;
    10.  
    11.    // If we press the button to scroll for the last item
    12.    scrollWithButtonToLast.performed += ChangeToLast;
    13. }
    14.  
    15. void ChangeToNext(InputAction.callbackcontext context)
    16. {
    17.    if (context.ReadValueAsButton())
    18.    {
    19.        scrollBar.value += changeValue;
    20.    }
    21. }
    22.  
    23. void ChangeToLast(InputAction.callbackcontext context)
    24. {
    25.    if (context.ReadValueAsButton())
    26.    {
    27.        scrollBar.value -= changeValue;
    28.    }
    29. }
    Dont forget to add nullchecks,...
     
    halivudestevez likes this.
  3. halivudestevez

    halivudestevez

    Joined:
    May 12, 2019
    Posts:
    14
    I add cooldown between the Scroll Perfom events.


    Code (CSharp):
    1. // To the head of the class, class fields
    2.     float lastTimeScrolled = 0;
    3.     float scrollCoolDown = 0.5f;

    Then at the perfom event:


    Code (CSharp):
    1.     private void Scroll_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
    2.     {
    3.         var v = obj.ReadValue<float>();
    4.         if (this.lastTimeScrolled + this.scrollCoolDown < Time.time)
    5.         {
    6.             this.lastTimeScrolled = Time.time;
    7.             if (v != 0)
    8.             {
    9.                 string vv = "";
    10.                 if (v > 0)
    11.                 {
    12.                     direction = "Up";
    13.                 }
    14.  
    15.                 if (v < 0)
    16.                 {
    17.                     direction = "Down";
    18.                 }
    19.  
    20.                 ScrollCounter++;
    21.                 Debug.Log($"Scroll performed: {vv} {ScrollCounter}");
    22.             }
    23.         }
    24.     }