- Home /
Mouse ScrollWheel Loop
Hi I'm having a little trouble understanding the logic in this script. Particularly the first if statment in the Update function. I've tried piecing it together but its somewhat obscure. Could someone explain it to me please?
#pragma strict
var currentWeapon = 0;
var maxWeapons = 2;
var theAnimator : Animator;
function Awake ()
{
}
function Update ()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0) //scrolling up
{
if(currentWeapon + 1 <= maxWeapons)
{
currentWeapon++;
}
else
{
currentWeapon = 0;
}
SelectWeapon(currentWeapon);
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if(currentWeapon - 1 >= 0)
{
currentWeapon--;
}
else
{
currentWeapon = maxWeapons;
}
SelectWeapon(currentWeapon);
}
if (currentWeapon == maxWeapons + 1)
{
currentWeapon = 0;
}
if (currentWeapon == -1)
{
currentWeapon = maxWeapons;
}
}
function SelectWeapon (index: int)
{
}
Answer by cryingwolf85 · May 26, 2014 at 03:49 PM
When you scroll up, Input.GetAxis("Mouse ScrollWheel") will return a positive value. When you scroll down it will return a negative value. At line 14, you are checking if Input.GetAxis has returned a positive value. If it has, that means that we scrolled up.
In this case, if we scroll up, we are changing the weapon that we are using with the index going up. If we scroll down we are decreasing the index.
The SelectWeapon function is the function you would use to physically change the weapon. Animations and such.
Line 39 to 47 are just checking to see if we scrolled out of the index. If so, you loop back through the weapon index. It is being set up to be used with an array of weapons.
Simple enough script. Good luck.