Добавить несколько различных видов оружия (2-3) с возможность переключения между ними.
This commit is contained in:
1 parent
87aaf3139a
commit
7a155dfb5e
11 files changed
+1651
-82
No files matched your search
@@ -1,6 +1,5 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
// ИЗМЕНЕНО: Используем пространство имен для TextMeshPro
|
||||
using TMPro;
|
||||
|
||||
public class PlayerShooting : MonoBehaviour
|
||||
@@ -19,7 +18,6 @@ public class PlayerShooting : MonoBehaviour
|
||||
|
||||
// --- Ссылки на компоненты и объекты ---
|
||||
public GameObject muzzleFlashSprite;
|
||||
// ИЗМЕНЕНО: Тип переменной заменен на TextMeshProUGUI
|
||||
public TextMeshProUGUI ammoText;
|
||||
public AudioClip reloadSound;
|
||||
|
||||
@@ -32,6 +30,15 @@ public class PlayerShooting : MonoBehaviour
|
||||
private AudioSource gunAudio;
|
||||
private Light gunLight;
|
||||
|
||||
// НОВЫЙ МЕТОД: Вызывается каждый раз, когда оружие становится активным
|
||||
void OnEnable()
|
||||
{
|
||||
// Прерываем перезарядку, если игрок переключился на это оружие
|
||||
isReloading = false;
|
||||
// Немедленно обновляем текст с количеством патронов
|
||||
UpdateAmmoText();
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
shootableMask = LayerMask.GetMask("Shootable");
|
||||
@@ -43,9 +50,9 @@ public class PlayerShooting : MonoBehaviour
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Инициализируем патроны только один раз при первом запуске
|
||||
currentAmmo = maxAmmo;
|
||||
UpdateAmmoText();
|
||||
|
||||
|
||||
if (gunLight != null) gunLight.enabled = false;
|
||||
if (muzzleFlashSprite != null) muzzleFlashSprite.SetActive(false);
|
||||
}
|
||||
@@ -151,7 +158,6 @@ public class PlayerShooting : MonoBehaviour
|
||||
{
|
||||
if (ammoText != null)
|
||||
{
|
||||
// Для TextMeshPro синтаксис точно такой же, так что здесь ничего менять не нужно
|
||||
ammoText.text = "Патроны: " + currentAmmo + " / " + maxAmmo;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 84d3169ccc0af7845ba1ae97fcd55584
|
||||
Reference in new issue
Block a user