Добавить несколько различных видов оружия (2-3) с возможность переключения между ними.
This commit is contained in:
1 parent
87aaf3139a
commit
7a155dfb5e
11 files changed
+1651
-82
No files matched your search
@@ -0,0 +1,73 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user