From c4eac172fe734b10501b80d67b11cf8e6d2bc23d Mon Sep 17 00:00:00 2001 From: Noah4ever <66632359+Noah4ever@users.noreply.github.com> Date: Sun, 21 Nov 2021 18:00:23 +0100 Subject: [PATCH] Weapon Switching/Pickup/Throwing + weapon switching works + weapon pickup works + weapon throwing works ntba weapon in correct position ntba weapon switch/pickup/throw animation --- .../Player/Player Animation Test.prefab | 2 +- .../Player/ProcedualAnimationController.cs | 12 +-- Assets/Scripts/Weapons/Shoot.cs | 9 +- Assets/Scripts/Weapons/WeaponManager.cs | 86 ++++++++++--------- 4 files changed, 59 insertions(+), 50 deletions(-) diff --git a/Assets/Prefabs/Player/Player Animation Test.prefab b/Assets/Prefabs/Player/Player Animation Test.prefab index c65f3aa..ed635a1 100644 --- a/Assets/Prefabs/Player/Player Animation Test.prefab +++ b/Assets/Prefabs/Player/Player Animation Test.prefab @@ -1455,7 +1455,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1880099159927430553, guid: 6f5ccb6e55f1676429c170257dc7a411, type: 3} propertyPath: m_IsActive - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5139256649524637864, guid: 6f5ccb6e55f1676429c170257dc7a411, type: 3} propertyPath: m_LocalPosition.x diff --git a/Assets/Scripts/Player/ProcedualAnimationController.cs b/Assets/Scripts/Player/ProcedualAnimationController.cs index 8b17142..3b6dd1e 100644 --- a/Assets/Scripts/Player/ProcedualAnimationController.cs +++ b/Assets/Scripts/Player/ProcedualAnimationController.cs @@ -110,12 +110,12 @@ public class ProcedualAnimationController : NetworkBehaviour { if (isLocalPlayer) { - //Play the animation - gunAnimator.Play("Shoot"); - //Add force for the recoil - //currentCameraRecoilX -= weightedPerlinNoise(cameraRecoilX, 1f, Time.time, 1); - //playerMouseLook = Mathf.SmoothDamp(playerMouseLook.fullPitch, playerMouseLook.fullPitch - weightedPerlinNoise(cameraRecoilX, 1f, Time.time, 1),ref f,0.01f); - CmdRecoil(); + //Play the animation + gunAnimator.Play("Shoot"); + //Add force for the recoil + //currentCameraRecoilX -= weightedPerlinNoise(cameraRecoilX, 1f, Time.time, 1); + //playerMouseLook = Mathf.SmoothDamp(playerMouseLook.fullPitch, playerMouseLook.fullPitch - weightedPerlinNoise(cameraRecoilX, 1f, Time.time, 1),ref f,0.01f); + CmdRecoil(); } } [Command] diff --git a/Assets/Scripts/Weapons/Shoot.cs b/Assets/Scripts/Weapons/Shoot.cs index b18da50..4f6e2b1 100644 --- a/Assets/Scripts/Weapons/Shoot.cs +++ b/Assets/Scripts/Weapons/Shoot.cs @@ -44,9 +44,12 @@ public class Shoot : NetworkBehaviour totalAmmo = weapon.TotalAmmunition; updateCanvas = false; } - if (Input.GetButtonDown("Fire")) { // BOX COLLIDER AUS + if (Input.GetButtonDown("Fire")) { updateCanvas = true; - Debug.Log(" click"); + if(weapon.GetComponent().enabled == true) // NACH ANDERE LÖSUNG SUCHEN + { + weapon.GetComponent().enabled = false; + } if (weapon.AllowAction && weapon.CurrentAmmunition > 0) { shootAnim.Recoil(0.1f); @@ -97,7 +100,7 @@ public class Shoot : NetworkBehaviour } } - void bulletHole(GameObject holeObject, RaycastHit hit) + void bulletHole(GameObject holeObject, RaycastHit hit) // Nur zum testen da { holeObject.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f); holeObject.transform.position = hit.point; diff --git a/Assets/Scripts/Weapons/WeaponManager.cs b/Assets/Scripts/Weapons/WeaponManager.cs index bdd4a7b..7719514 100644 --- a/Assets/Scripts/Weapons/WeaponManager.cs +++ b/Assets/Scripts/Weapons/WeaponManager.cs @@ -27,48 +27,20 @@ public class WeaponManager : NetworkBehaviour if (isLocalPlayer) { if(Input.GetAxis("Mouse ScrollWheel") > 0f){ // Scroll up lastWeaponIndex = currentWeaponIndex; - counter = 0; - do { - if (currentWeaponIndex <= 0) { - currentWeaponIndex = activeWeapons.Count - 1; - } else { - currentWeaponIndex--; - } - counter++; - if (counter > 10) { break; } - } while (activeWeapons[currentWeaponIndex] == null); - if (lastWeaponIndex != currentWeaponIndex && activeWeapons[currentWeaponIndex] != null) - { - // play switch animation or move weapon (hands) down - foreach (GameObject obj in activeWeapons) - { // Disable all weapons - if (obj != null) { obj.SetActive(false); } - } - Debug.Log("Set Active (" + currentWeaponIndex + "): " + activeWeapons[currentWeaponIndex].name); + int nextActive = SearchForNext(activeWeapons, lastWeaponIndex, -1); + if (nextActive != -1) { // -1 no next found + currentWeaponIndex = nextActive; activeWeapons[currentWeaponIndex].SetActive(true); + // play weapon switch animation } } else if (Input.GetAxis("Mouse ScrollWheel") < 0f){ // Scroll down lastWeaponIndex = currentWeaponIndex; - counter = 0; - do { - if (currentWeaponIndex >= activeWeapons.Count - 1) { - currentWeaponIndex = 0; - } else { - currentWeaponIndex++; - } - counter++; - if(counter > 10) { break; } - } while (activeWeapons[currentWeaponIndex] == null); - if (lastWeaponIndex != currentWeaponIndex && activeWeapons[currentWeaponIndex] != null) - { - // play switch animation or move weapon (hands) down - foreach (GameObject obj in activeWeapons) - { // Disable all weapons - if (obj != null) { obj.SetActive(false); } - } - Debug.Log("Set Active (" + currentWeaponIndex + "): " + activeWeapons[currentWeaponIndex].name); + int nextActive = SearchForNext(activeWeapons, lastWeaponIndex, 1); + if (nextActive != -1) { // -1 no next found + currentWeaponIndex = nextActive; activeWeapons[currentWeaponIndex].SetActive(true); + // play weapon switch animation } } @@ -78,22 +50,56 @@ public class WeaponManager : NetworkBehaviour }else if (Input.GetButtonDown("Drop")) // q Droping weapon { - // WENN GEDROPT WIRD MUSS DIE NÄCHSTE AKTIVE GSUCHT WERDEn if(activeWeapons[currentWeaponIndex] != null) { - activeWeapons[currentWeaponIndex].GetComponent().useGravity = true; - activeWeapons[currentWeaponIndex].GetComponent().isKinematic = false; + Rigidbody rigid = activeWeapons[currentWeaponIndex].GetComponent(); + rigid.useGravity = true; + rigid.isKinematic = false; activeWeapons[currentWeaponIndex].transform.position = cam.transform.position; activeWeapons[currentWeaponIndex].GetComponent().enabled = true; - activeWeapons[currentWeaponIndex].GetComponent().velocity = cam.transform.forward * 10 + cam.transform.up * 2; + rigid.velocity = cam.transform.forward * 10 + cam.transform.up * 2; activeWeapons[currentWeaponIndex].gameObject.transform.SetParent(null); activeWeapons[currentWeaponIndex] = null; + + int nextActive = SearchForNext(activeWeapons, currentWeaponIndex, 1); + if(nextActive != -1) // -1 no next found + { + lastWeaponIndex = currentWeaponIndex; + currentWeaponIndex = nextActive; + activeWeapons[currentWeaponIndex].SetActive(true); + } + } + } + } } + private int SearchForNext(List l, int lastActive = 0, int direction = 1) { + int size = l.Count-1; + bool condition = true; + if (lastActive <= -1) { lastActive = size; } + if(lastActive >= l.Count) { lastActive = 0; } + for (int i = lastActive+direction; condition; i+= direction) { + if (i >= l.Count-1) { i = 0; size = lastActive+1; } + else if(i < 0) { i = size; size = -1; } + if (l[i] != null) { + if(l[lastActive] != null) { l[lastActive].SetActive(false); } + return i; + } + if (direction == 1) { + if (i < size + 1) { condition = true; } + else { condition = false; } + }else if(direction == -1) { + if (i > size + 1) { condition = true; } + else { condition = false; } + } + } + return -1; + } + [Command] private void CmdPickupWeapon() { if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit))