From e8b0617dbbdbd880010ea88db3dd5732b4142f68 Mon Sep 17 00:00:00 2001 From: Stedd Date: Mon, 24 Oct 2022 22:16:48 +0200 Subject: [PATCH] fixed weapon switching bug Script was on 2 gameobjects --- Assets/Scenes/Sandbox.unity | 21 +----------------- Assets/Weapons/WeaponSwitcher.cs | 38 +++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/Assets/Scenes/Sandbox.unity b/Assets/Scenes/Sandbox.unity index a381525..510adfc 100644 --- a/Assets/Scenes/Sandbox.unity +++ b/Assets/Scenes/Sandbox.unity @@ -3883,36 +3883,17 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 45e653bab7fb20e499bda25e1b646fea, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!1 &1859157918 stripped -GameObject: - m_CorrespondingSourceObject: {fileID: 8616685848737228372, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} - m_PrefabInstance: {fileID: 8204767108263278737} - m_PrefabAsset: {fileID: 0} --- !u!114 &1859157935 stripped MonoBehaviour: m_CorrespondingSourceObject: {fileID: 5420857361349278248, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} m_PrefabInstance: {fileID: 8204767108263278737} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1859157918} + m_GameObject: {fileID: 0} m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: e087ecce43ebbff45a1b360637807d93, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!114 &1859157939 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1859157918} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 51cf941944fb27e43863c290683f7068, type: 3} - m_Name: - m_EditorClassIdentifier: - _weapons: [] - _currentWeapon: 0 --- !u!20 &1874573760 stripped Camera: m_CorrespondingSourceObject: {fileID: 9005220659476430821, guid: 2d3a85ecde41a8246a79669975912b74, type: 3} diff --git a/Assets/Weapons/WeaponSwitcher.cs b/Assets/Weapons/WeaponSwitcher.cs index 92bd3b8..4c53f7e 100644 --- a/Assets/Weapons/WeaponSwitcher.cs +++ b/Assets/Weapons/WeaponSwitcher.cs @@ -30,32 +30,48 @@ public class WeaponSwitcher : MonoBehaviour { foreach (Weapon weapon in _weapons) { - if (weapon.Index == _currentWeapon) + weapon.gameObject.SetActive(false); + if (weapon.Index == index) { weapon.gameObject.SetActive(true); } } } - private void DeselectAllWeapons() - { - foreach (Weapon weapon in _weapons) - { - weapon.gameObject.SetActive(false); - } - } - private void Update() { if (Mouse.current.middleButton.wasPressedThisFrame) { - _currentWeapon += 1; + _currentWeapon++; if (_currentWeapon > _weapons.Length - 1) { _currentWeapon = 0; } - DeselectAllWeapons(); + SelectWeapon(_currentWeapon); + } + + if (Keyboard.current.digit1Key.wasPressedThisFrame) + { + _currentWeapon = 0; + SelectWeapon(_currentWeapon); + } + + if (Keyboard.current.digit2Key.wasPressedThisFrame) + { + _currentWeapon = 1; + SelectWeapon(_currentWeapon); + } + + if (Keyboard.current.digit3Key.wasPressedThisFrame) + { + _currentWeapon = 2; + SelectWeapon(_currentWeapon); + } + + if (Keyboard.current.digit4Key.wasPressedThisFrame) + { + _currentWeapon = 3; SelectWeapon(_currentWeapon); } }