74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
using UnityEngine;
|
|
|
|
public class WeaponSwitcher : MonoBehaviour
|
|
{
|
|
// Массив, в котором будут храниться все наши объекты-оружия
|
|
public GameObject[] weapons;
|
|
private int currentWeaponIndex = 0;
|
|
|
|
void Start()
|
|
{
|
|
// При запуске игры выбираем первое оружие
|
|
SwitchWeapon(0);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// --- Переключение с помощью цифровых клавиш ---
|
|
if (Input.GetKeyDown(KeyCode.Alpha1))
|
|
{
|
|
SwitchWeapon(0);
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Alpha2) && weapons.Length >= 2)
|
|
{
|
|
SwitchWeapon(1);
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Alpha3) && weapons.Length >= 3)
|
|
{
|
|
SwitchWeapon(2);
|
|
}
|
|
|
|
// --- Переключение с помощью колеса мыши ---
|
|
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
|
if (scroll > 0f) // Прокрутка вверх
|
|
{
|
|
currentWeaponIndex++;
|
|
if (currentWeaponIndex >= weapons.Length)
|
|
{
|
|
currentWeaponIndex = 0; // Возвращаемся к первому
|
|
}
|
|
SwitchWeapon(currentWeaponIndex);
|
|
}
|
|
else if (scroll < 0f) // Прокрутка вниз
|
|
{
|
|
currentWeaponIndex--;
|
|
if (currentWeaponIndex < 0)
|
|
{
|
|
currentWeaponIndex = weapons.Length - 1; // Переходим к последнему
|
|
}
|
|
SwitchWeapon(currentWeaponIndex);
|
|
}
|
|
}
|
|
|
|
void SwitchWeapon(int newIndex)
|
|
{
|
|
// Проверяем, что индекс в пределах массива
|
|
if (newIndex < 0 || newIndex >= weapons.Length)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Выключаем все оружия
|
|
for (int i = 0; i < weapons.Length; i++)
|
|
{
|
|
weapons[i].SetActive(false);
|
|
}
|
|
|
|
// Включаем только нужное
|
|
weapons[newIndex].SetActive(true);
|
|
|
|
// Обновляем текущий индекс
|
|
currentWeaponIndex = newIndex;
|
|
}
|
|
}
|