Switching Weapons with middlemouse- merging two code samples

Hiya so as thread title states, Im looking to switch weapons with the middlemouse scroll functionality. This is an example of how get Input:

   // Handles Zoom In  Out
   if (Input.GetAxis("Mouse ScrollWheel") < 0){
      if (height + 2 <= maxHeight){
         height += 2;
      }
      
      if (distance + 2 <= maxDist){
         distance += 2;
      }
   } else if (Input.GetAxis("Mouse ScrollWheel") > 0){
      if (height - 2 >= minHeight){
         height -= 2;
      }
      
      if (distance - 2 >= minDist){
         distance -= 2;
      }
   } //This is a zoom functionality

And then this is the current code for weapon switching;

function Awake () {
	// Select the first weapon
	SelectWeapon(0);
}


function Update () {
	// Did the user press fire?
	if (Input.GetButton ("Fire1"))
		BroadcastMessage("Fire");
	
	if (Input.GetKeyDown("1"))
	{
		SelectWeapon(0);
	}	
	else if (Input.GetKeyDown("2"))
	{
		SelectWeapon(1);
	}	
}

function SelectWeapon (index : int) {
	for (var i=0;i<transform.childCount;i++)
	{
		// Activate the selected weapon
		if (i == index)
			transform.GetChild(i).gameObject.SetActiveRecursively(true);
		// Deactivate all other weapons
		else
			transform.GetChild(i).gameObject.SetActiveRecursively(false);
	}
}

It would be good to keep the 1 and 2 key functionality if possible but tracking whats done what and how to setup middlemouse scroll is beyond me. Perhaps theres an easier way?

Thanks for any advice, a switch weapons snip using middlemouse scroll could be a good thing for the Wiki too…

AaronC

Not too complicated really, the two scripts you posted give you all the logic you need. You just substitute height and max/minHeight with an index for the selected weapon and the total number of weapons to select from.

if (Input.GetAxis("Mouse ScrollWheel") < 0){ 
	if (currentWeapon + 1 <= numWeapons){ 
	currentWeapon++;
	} else {
	currentWeapon = 0;
	}
	SelectWeapon(currentWeapon);
} else if (Input.GetAxis("Mouse ScrollWheel") > 0){ 
	if (currentWeapon - 1 >= 0){ 
	currentWeapon--;
	} else {
	currentWeapon = numWeapons;
	}
	SelectWeapon(currentWeapon);
}

I also added in a final else statement to both blocks to loop the index around if it goes out of range instead of just ignoring it. The above script could probably use some tweaking as I haven’t tested it, but it should just slot into the weapon switching script you posted.

Awesome, thank you very much, I just needed to add a couple of int var’s, and it works. This is the sort of snippet I need to glance at occasionally for maybe a few weeks and then the penny will drop and I’ll hopefully be able to read it like english…

Most appreciated.

Heres PlayerWeapons.js for the Fps tutorial, Unity2.5 compatible- If UT wants to grab this- it works.

AaronC

	var currentWeapon = 0; 
	var numWeapons = 1; 
	function Awake () {
	// Select the first weapon
	SelectWeapon(0);
}


function Update () {

	// Did the user press fire?
	if (Input.GetButton ("Fire1"))
		BroadcastMessage("Fire");
		
	if (Input.GetAxis("Mouse ScrollWheel") < 0){
		
 	  if (currentWeapon + 1 <= numWeapons){
	   currentWeapon++;
	   } else {
 
 	  currentWeapon = 0;
  	
  		 }
		   SelectWeapon(currentWeapon);
			} else if (Input.GetAxis("Mouse ScrollWheel") > 0){
 			
 			  if (currentWeapon - 1 >= 0){
  		 currentWeapon--;
  		 
 		  } else {
   currentWeapon = numWeapons;
   }
   SelectWeapon(currentWeapon);
}
	if (Input.GetKeyDown("1"))
	{
		SelectWeapon(0);
	}	
	else if (Input.GetKeyDown("2"))
	{
		SelectWeapon(1);
	}	
}

function SelectWeapon (index : int) {
	for (var i=0;i<transform.childCount;i++)
	{
		// Activate the selected weapon
		if (i == index)
			transform.GetChild(i).gameObject.SetActiveRecursively(true);
		// Deactivate all other weapons
		else
			transform.GetChild(i).gameObject.SetActiveRecursively(false);
	}
}