From eaee767535c51cdb0830e247b1ac85776f562ac6 Mon Sep 17 00:00:00 2001 From: Noah4ever <66632359+Noah4ever@users.noreply.github.com> Date: Tue, 16 Nov 2021 13:05:27 +0100 Subject: [PATCH] Weapon Switching + started weapon switching + added only get active weapon (if you have rifle and pistole and you drop rifle you cant scroll to rifle slot) --- Assets/Scripts/Weapons/Shoot.cs | 3 +- Assets/Scripts/Weapons/WeaponManager.cs | 53 +++++++++++++++++-------- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/Assets/Scripts/Weapons/Shoot.cs b/Assets/Scripts/Weapons/Shoot.cs index 93a67c5..6c86b53 100644 --- a/Assets/Scripts/Weapons/Shoot.cs +++ b/Assets/Scripts/Weapons/Shoot.cs @@ -47,11 +47,12 @@ public class Shoot : NetworkBehaviour if (Input.GetButtonDown("Fire")) { updateCanvas = true; Debug.Log(" click"); - CmdFireBullet(); if (weapon.AllowAction) { shootAnim.Recoil(0.1f); } + CmdFireBullet(); + } if (Input.GetButtonDown("Reload")) { updateCanvas = true; diff --git a/Assets/Scripts/Weapons/WeaponManager.cs b/Assets/Scripts/Weapons/WeaponManager.cs index 911df42..e628fa2 100644 --- a/Assets/Scripts/Weapons/WeaponManager.cs +++ b/Assets/Scripts/Weapons/WeaponManager.cs @@ -8,24 +8,52 @@ public class WeaponManager : NetworkBehaviour { public int currentWeaponIndex = 0; public GameObject[] activeWeapons; + private int counter = 0; [SerializeField] Camera cam; private void Awake() { activeWeapons = new GameObject[4]; + for(int i = 0; i<4; i++) + { + activeWeapons[i] = null; + } } void Update() { if (isLocalPlayer) { + counter = 0; if(Input.GetAxis("Mouse ScrollWheel") > 0f){ // Scroll up - if (currentWeaponIndex <= 0) - { currentWeaponIndex = activeWeapons.Length; } - else { currentWeaponIndex--; } - }else if (Input.GetAxis("Mouse ScrollWheel") < 0f){ // Scroll down - if (currentWeaponIndex >= activeWeapons.Length) - { currentWeaponIndex = 0; } - else { currentWeaponIndex++; } + do + { + if (currentWeaponIndex <= 0) + { + currentWeaponIndex = activeWeapons.Length - 1; + } + else + { + currentWeaponIndex--; + } + counter++; + Debug.Log(activeWeapons[currentWeaponIndex]); + } while (activeWeapons[currentWeaponIndex] == null); + } + else if (Input.GetAxis("Mouse ScrollWheel") < 0f){ // Scroll down + do + { + if (currentWeaponIndex >= activeWeapons.Length - 1) + { + currentWeaponIndex = 0; + } + else + { + currentWeaponIndex++; + } + counter++; + Debug.Log(activeWeapons[currentWeaponIndex]); + } while (activeWeapons[currentWeaponIndex] == null); + } if (Input.GetButton("Interact")) // e @@ -45,7 +73,7 @@ public class WeaponManager : NetworkBehaviour { if (hit.transform.tag == "Weapon") // If Object is a weapon and the weapon is not in the current active weapons { - Destroy(hit.transform.gameObject); + switch (hit.transform.GetComponent().WeaponKind.ToString()) // Adding weapon to inventory slot { case "Rifle": activeWeapons[0] = hit.transform.gameObject; break; @@ -58,13 +86,4 @@ public class WeaponManager : NetworkBehaviour } } - private bool isInArray(GameObject[] arr, GameObject searchObj) - { - foreach(GameObject obj in arr) - { - if (obj == searchObj) return true; - } - return false; - } - }