- Home /
Question by
SalDaDoor · May 19, 2021 at 05:23 PM ·
2d gameweapon system
Switching Weapons with Weapon Wheel
I'm very new to coding and I'm simply trying to enable one weapon while disabling the others by pressing a button on my weapon wheel. The weapon wheel works, I just can't find any good info on this specific topic.
using UnityEngine.UI;
using UnityEngine;
public class WeaponWheelController : MonoBehaviour
{
public Animator anim;
private bool weaponWheelSelected = false;
public Image selectedItem;
public Sprite noImage;
public static int weaponID;
int totalWeapons = 1;
public int currentWeaponIndex;
public GameObject[] guns;
public GameObject weaponHolder;
public GameObject currentGun;
void Start()
{
totalWeapons = weaponHolder.transform.childCount;
guns = new GameObject[totalWeapons];
for (int i = 0; i < totalWeapons; i++)
{
guns[i] = weaponHolder.transform.GetChild(i).gameObject;
guns[i].SetActive(false);
}
guns[0].SetActive(true);
currentGun = guns[0];
currentWeaponIndex = 0;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
weaponWheelSelected = !weaponWheelSelected;
}
if (weaponWheelSelected)
{
anim.SetBool("OpenWeaponWheel", true);
}
else
{
anim.SetBool("OpenWeaponWheel", false);
}
switch (weaponID)
{
case 0: //nothing is selected
selectedItem.sprite = noImage;
break;
case 1: //pistol
Debug.Log("Pistol");
break;
case 2: //rifle
Debug.Log("Rifle");
break;
case 3: //shotgun
Debug.Log("Shotgun");
break;
case 4: //plasma
Debug.Log("LaserReflector");
break;
}
}
}
Comment
Answer by Casiell · May 19, 2021 at 08:49 PM
By "the weapon wheel works" you mean opening the weapon wheel with tab? Or do you also have the code for selecting a weapon in the wheel?
I would imagine that each weapon shown in the wheel would be a button. When you click the button you call a method with a weapon ID corresponding to weapon image. This method would do the whole weapon swapping thing, so probably what you do in start with weapon 0.