add: weapon switching system

This commit is contained in:
KOSMOGOR
2025-04-18 14:05:45 +03:00
parent 39bc7aceb1
commit 28a8939ab7
2 changed files with 24 additions and 0 deletions
+17
View File
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class WeaponController : MonoBehaviour
@@ -12,6 +13,22 @@ public class WeaponController : MonoBehaviour
}
void Update() {
ChangeActiveWeapon((currentWeapon + (int)Input.mouseScrollDelta.y) % weapons.Length);
CheckButtonsForChangeWeapon();
if (Input.GetMouseButton(0) && weapons[currentWeapon].isActive) weapons[currentWeapon].Shoot(Input.GetMouseButtonDown(0), Input.GetMouseButton(0));
}
void CheckButtonsForChangeWeapon() {
KeyCode[] keys = {KeyCode.Alpha1, KeyCode.Alpha2, KeyCode.Alpha3, KeyCode.Alpha4, KeyCode.Alpha5};
foreach (var (key, i) in keys.Select((val, ind) => (val, ind))) {
if (Input.GetKeyDown(key)) ChangeActiveWeapon(i);
}
}
void ChangeActiveWeapon(int ind) {
if (ind < 0 || ind >= weapons.Length || ind == currentWeapon) return;
weapons[currentWeapon].SetWeaponState(false);
weapons[ind].SetWeaponState(false);
currentWeapon = ind;
}
}