New input system + new asset pack vs this lecture

As some of us are trying to work with new StarterAssets (I use FirstPerson pack) and new input system, there might be some wonders how to solve weapon scrolling, or weapon selection.

Here I attach solution I worked out. Of course it might not be the best approach, but at least it works for me. Hope this will help somebody :slight_smile:

WeaponSwitcher.cs

private void ProcessScrollWeapon()
    {
        if(_input.scrollWeapon.y!=0)
        {
            if (_input.scrollWeapon.y < 0)
            {
                if (currentWeapon >= transform.childCount - 1)
                {
                    currentWeapon = 0;
                }
                else
                {
                    currentWeapon++;
                }
            }
            else if (_input.scrollWeapon.y > 0)
            {
                if (currentWeapon <= 0)
                {
                    currentWeapon = transform.childCount - 1;
                }
                else
                {
                    currentWeapon--;
                }
            }
        }
        _input.scrollWeapon.y = 0;
    }

private void ProcessSelectWeapon()
    {
        if (_input.firstWeapon)
        {
            currentWeapon = 0;
        }
        else if (_input.secondWeapon)
        {
            currentWeapon = 1;
        }
        else if (_input.thirdWeapon)
        {
            currentWeapon = 2;
        }
        _input.firstWeapon = false;
        _input.secondWeapon = false;
        _input.thirdWeapon = false;
    }
StarterAssetsInput.cs

public bool firstWeapon;
public bool secondWeapon;
public bool thirdWeapon;
public Vector2 scrollWeapon;

        public void OnFirstWeapon(InputValue value)
		{
			FirstWeaponInput(value.isPressed);
		}

		public void OnSecondWeapon(InputValue value)
		{
			SecondWeaponInput(value.isPressed);
		}

		public void OnThirdWeapon(InputValue value)
		{
			ThirdWeaponInput(value.isPressed);
		}

		public void OnScrollWeapon(InputValue value)
		{
			ScrollWeaponInput(value.Get<Vector2>());
		}

        public void FirstWeaponInput(bool newFirstWeaponState)
		{
			firstWeapon = newFirstWeaponState;
		}

		public void SecondWeaponInput(bool newSecondWeaponState)
		{
			secondWeapon = newSecondWeaponState;
		}

		public void ThirdWeaponInput(bool newThirdWeaponState)
		{
			thirdWeapon = newThirdWeaponState;
		}
		public void ScrollWeaponInput(Vector2 newScrollWeaponState)
		{
			scrollWeapon = newScrollWeaponState;
		}

First / Second / Third weapons are Buttons.
For scroll we can get delta Value as Vector2, so we don’t have to check if we are scrolling Up or Down.

I did struggle with the new input system for a couple of lectures, this part also gave me some troubles. GJ with making this work, as it really is not so easy for beginners like me :wink:
I didn’t like this solution, as (as OP probably feels as well) going with 3 actions for every weapon seems not elegant nor easily scalable. I wanted to have 1 action, and 3 bindings, like that you can easily expand by adding other bindings (i.e. keys 4, 5, 6 etc) if you need it.

It turns out, you can do it. The way i found is to set action to value type and than ‘any’ control type
obraz

It allows a value to be passed, and it works like this: pressed key passes ‘1’, on release it passes ‘0’.
Than what you can do is apply scale processor on every key, like this:
obraz

Scroll is the same as in OP. (Just remember it has to be value Action type, or you can’t add scroll wheel. It took me couple minutes to switch it from button… :sweat_smile: )

To receive that in script, I did this in StarterAssetsInputs:

		public void OnWeaponSelectionKey(InputValue value)
		{
			WeaponSelecion(value.Get<float>());
		}

		public void OnMouseScroll(InputValue value)
		{
			MouseScrollSelection(value.Get<Vector2>());
		}

Important fact is, that key value (1 or 0) is passed as float.
And later it is processed:

public void WeaponSelecion(float weaponSelection)
		{
		if (weaponSelection > 0)
			{
				selectedWeapon = (int)weaponSelection;
			}
		}

public void MouseScrollSelection(Vector2 vector)
		{			
			if(vector.y > 0)
			{
				selectedWeapon++;
			}
			else if( vector.y < 0)
			{
				selectedWeapon--;	
			}
		}

Because of weaponSelection being float, it is convenient to convert it to int.
weaponSelection >0 is checked to filter out 0 coming from key releases. In theory it should be available in Action or Binding Interaction panel (add interaction to gather only press), but for whatever reason it doesn’t work like this.

This approach allows to use only 1 method in WeaponSwitcher script: ProcessWeaponSwitch();
You don’t need to process scroll or keypress individually, as it is already processed in StarterAssetsInputs. (Probably it would be better to process it in another class, like CharacterController, to keep Inputs only as Inputs, and keep processing in different place, but for now I think I’m good with this…)

My solution for switching goes like this:

    private void ProcessWeaponSwitch()
    { 
        if (starterAssetsInputs.selectedWeapon < 0)
        {
            starterAssetsInputs.selectedWeapon = 0;
        }
        else if (starterAssetsInputs.selectedWeapon > transform.childCount -1)
        {
            starterAssetsInputs.selectedWeapon = transform.childCount;
        }

        currentWeapon = starterAssetsInputs.selectedWeapon;
    }

(You can’t scroll past weapon 0, or last weapon in this example. Scroll doesn’t loop.)
And I am not fond of doing this scroll check that way, as I don’t like overwriting selectedWeapon int from another class, I guess it will stay.