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
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.