- Home /
mouse wheel scroll weapon switch
hey guys how can i change this in to mousewheel scroll up and down?
using UnityEngine;
using System.Collections;
public class SwitchWeapons : MonoBehaviour
{
public GameObject[] weapons; // push your prefabs
public int currentWeapon = 0;
private int nrWeapons;
void Start()
{
nrWeapons = weapons.Length;
SwitchWeapon(currentWeapon); // Set default gun
}
void Update()
{
for (int i = 1; i <= nrWeapons; i++)
{
if (Input.GetKeyDown("" + i))
{
currentWeapon = i - 1;
SwitchWeapon(currentWeapon);
}
}
}
void SwitchWeapon(int index)
{
for (int i = 0; i < nrWeapons; i++)
{
if (i == index)
{
weapons[i].gameObject.SetActive(true);
}
else {
weapons[i].gameObject.SetActive(false);
}
}
}
public void IsRocketLouncher()
{
currentWeapon = 0;
nrWeapons = weapons.Length;
SwitchWeapon(currentWeapon);
}
}
Answer by Peysbubby · Feb 29, 2016 at 05:45 PM
Hello, my following answer may not be perfect, and it needs some polish, but I hope this at least helps you get an idea of what you need.
public int currentWeapon;
public GameObject activeWeapon;
public GameObject gun, rifle, shotgun, rocketLauncher;// list of weapons
public bool weaponIsSwitching;
void Start()
{
weaponIsSwitching = true;
}
void Update()
{
float w = Input.GetAxis("Mouse ScrollWheel");
if (w > 0f)// if scrollwheel has moved up
{
currentWeapon = currentWeapon + 1;
weaponIsSwitching = true; // Setting the bool true here and setting it false after the weapon has changed prevents instantiating more than one instance of the same object.
}
else if (w < 0f)// if scrollwheel haas moved down
{
currentWeapon = currentWeapon - 1;
weaponIsSwitching = true; // Setting the bool true here and setting it false after the weapon has changed prevents instantiating more than one instance of the same object.
}
if (weaponIsSwitching == true)
{
// This switch does the job of actually changing the weapon.
// Each case would represent a different weapon.
switch (currentWeapon)
{
// When adding a case just be sure to change the prefab (gun, rifle, shotgun) to match whatever weapon you are adding, and increasing the case number by 1 for each case.
case 0: // Instantiates a weapon named gun
Destroy (activeWeapon); // Destroys the current weapon in the scene, ensuring that two weapons are not in the scene at the same time.
activeWeapon = Instantiate (gun, new Vector3 (activeWeapon.transform.position.x, activeWeapon.transform.position.y, activeWeapon.transform.position.z), Quaternion.identity) as GameObject;
weaponIsSwitching = false;
break;
case 1: // Instantiates a weapon named rifle
Destroy (activeWeapon); // Destroys the current weapon in the scene, ensuring that two weapons are not in the scene at the same time.
activeWeapon = Instantiate (rifle, new Vector3 (activeWeapon.transform.position.x, activeWeapon.transform.position.y, activeWeapon.transform.position.z), Quaternion.identity) as GameObject;
weaponIsSwitching = false;
break;
case 2: // Instantiates a weapon named shotgun
Destroy (activeWeapon); // Destroys the current weapon in the scene, ensuring that two weapons are not in the scene at the same time.
activeWeapon = Instantiate (shotgun, new Vector3 (activeWeapon.transform.position.x, activeWeapon.transform.position.y, activeWeapon.transform.position.z), Quaternion.identity) as GameObject;
weaponIsSwitching = false;
break;
}
}
// The following ensures the weapon int never exceeds your amount of weapons.
if (currentWeapon > 2)// Make sure the number here always mathces the amount of cases in the switch above.
{
currentWeapon = 0; // Leave this number the same
}
if (currentWeapon < 0) // Leave this number the same
{
currentWeapon = 2;// Make sure the number here always mathces the amount of cases in the switch above.
}
}
You will need to create an empty gameObject in the scene for the activeWeapon object. Your weapon will instantiate here, so make sure you place this item where you want your weapon.
If you use this script and make sure everything is assigned in the inspector you should be good to go.
I hope this helps.
Hey there @zoomyjs ! Glad that this script could help you. No problem.
Your answer
Follow this Question
Related Questions
RTS Camera - Zoom is not uniform, yet code is? 2 Answers
Light intensity with mouse scrollwheel 1 Answer
Magic mouse scroll-wheel 1 Answer
My Mouse ScrollWheel input is missing! 1 Answer
Edge Scroll Problem 1 Answer