So I watched some tutorials and made a inventory and weapon system but the problem is these system were both taken from different kind of videos. So I want to combine both of them.
this is my weapon switch system
using UnityEngine;
public class Weapon_switching : MonoBehaviour
{
public int selectedWeapon = 0;
// Start is called before the first frame update
void Start()
{
SelectWeapon();
}
// Update is called once per frame
void Update()
{
int previousSelectedWeapon = selectedWeapon;
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount - 1;
else
selectedWeapon--;
}
if(Input.GetKeyDown(KeyCode.Alpha1))
{
selectedWeapon = 0;
}
if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2)
{
selectedWeapon = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
{
selectedWeapon = 2;
}
if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 4)
{
selectedWeapon = 3;
}
if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 5)
{
selectedWeapon = 4;
}
if (previousSelectedWeapon != selectedWeapon)
{
SelectWeapon();
}
}
void SelectWeapon ()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
}
}
}
this is the equipment code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Equipment", menuName = "Inventory/Equipment")]
public class Equipment : Item
{
public EquipmentSlot equipSlot;
public int armorModifier;
public override void Use()
{
base.Use();
EquipmentManager.instance.Equip(this);
RemoveFromInventory();
}
}
public enum EquipmentSlot { Armor, Smg, Pistol, Rifle, Heavy}
this is the equipment manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipmentManager : MonoBehaviour
{
#region Singleton
public static EquipmentManager instance;
private void Awake()
{
instance = this;
}
#endregion
Equipment[] currentEquipment;
public delegate void OnEquipmentChanged(Equipment newItem, Equipment oldItem);
public OnEquipmentChanged onEquipmentChanged;
Inventory inventory;
private void Start()
{
inventory = Inventory.instance;
int numSlots = System.Enum.GetNames(typeof(EquipmentSlot)).Length;
currentEquipment = new Equipment[numSlots];
}
public void Equip (Equipment newItem)
{
int slotIndex = (int)newItem.equipSlot;
Equipment oldItem = null;
if (currentEquipment[slotIndex] != null)
{
oldItem = currentEquipment[slotIndex];
inventory.Add(oldItem);
}
if (onEquipmentChanged != null)
{
onEquipmentChanged.Invoke(newItem, oldItem);
}
currentEquipment[slotIndex] = newItem;
}
}
I wanna make it so when I press 1 on the keyboard it will equip the smg and pistol when I press 2 and be switchable with the mouse scrollwheel too
I am new at the game making stuff and still trying to learn and I would be very happy if someone could help me.
edit: forgot to say, I want to abandon the old method and switch between my weapons using the equipment system