add: weapon switching system
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7a6da81b74a2fb344a8f0893d21193cf
|
||||||
|
TextScriptImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class WeaponController : MonoBehaviour
|
public class WeaponController : MonoBehaviour
|
||||||
@@ -12,6 +13,22 @@ public class WeaponController : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Update() {
|
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));
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user