PlayerInput - using scroll Up/Down as a button

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?

Just do the following (but you need to create a new InputAction with type Button):

public InputAction scrollWithButtonToNext;
public InputAction scrollWithButtonToLast;
public Scrollbar scrollBar;
public float changeValue = 0.1f;

void OnEnable()
{
   // If we press the button to scroll for the next item
   scrollWithButtonToNext.performed += ChangeToNext;

   // If we press the button to scroll for the last item
   scrollWithButtonToLast.performed += ChangeToLast;
}

void ChangeToNext(InputAction.callbackcontext context)
{
   if (context.ReadValueAsButton())
   {
       scrollBar.value += changeValue;
   }
}

void ChangeToLast(InputAction.callbackcontext context)
{
   if (context.ReadValueAsButton())
   {
       scrollBar.value -= changeValue;
   }
}

Dont forget to add nullchecks,…

1 Like

I add cooldown between the Scroll Perfom events.

// To the head of the class, class fields
    float lastTimeScrolled = 0;
    float scrollCoolDown = 0.5f;

Then at the perfom event:

    private void Scroll_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
    {
        var v = obj.ReadValue<float>();
        if (this.lastTimeScrolled + this.scrollCoolDown < Time.time)
        {
            this.lastTimeScrolled = Time.time;
            if (v != 0)
            {
                string vv = "";
                if (v > 0)
                {
                    direction = "Up";
                }

                if (v < 0)
                {
                    direction = "Down";
                }

                ScrollCounter++;
                Debug.Log($"Scroll performed: {vv} {ScrollCounter}");
            }
        }
    }