Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class WeaponSwitching : MonoBehaviour
- {
- //Tracks what weapon the player currently has equipped
- public int SelectedWeapon = 1;
- //The players Shotgun, PulseRifle and RocketLauncher weapons that will be turned on and off while switching between them
- [SerializeField]
- GameObject Shotgun, PulseRifle, RocketLauncher;
- //Player starts with the Shotgun weapon equipped
- void Start()
- {
- SwapWeapon(1);
- {
- SwapWeapon(1);
- }
- }
- void Update()
- {
- //Pressing 1 swaps to the Shotgun
- if (Input.GetKeyDown(KeyCode.Alpha1))
- {
- Debug.Log("Trying to equip the Shotgun");
- //If they already have the Shotgun equipped do nothing
- if (SelectedWeapon == 1)
- {
- Debug.Log("Shotgun is already equipped.");
- return;
- }
- //If they dont have access to the Shotgun do nothing
- if (!WeaponManager.Instance.ShotgunAcquired)
- {
- Debug.Log("You dont have the Shotgun weapon.");
- return;
- }
- //Disable the current weapon, then enable the Shotgun weapon
- Debug.Log("Equipping the Shotgun weapon now");
- ToggleWeapon(SelectedWeapon, false);
- ToggleWeapon(1, true);
- SelectedWeapon = 1;
- }
- //Pressing 2 swaps to the PulseRifle
- if (Input.GetKeyDown(KeyCode.Alpha2))
- {
- Debug.Log("Trying to equip the PulseRifle");
- //If they already have the PulseRifleequipped do nothing
- if (SelectedWeapon == 2)
- {
- Debug.Log("PulseRifle is already equipped.");
- return;
- }
- //If they dont have access to the PulseRifle do nothing
- if (!WeaponManager.Instance.PulseRifleAcquired)
- {
- Debug.Log("You dont have the PulseRifle weapon.");
- return;
- }
- //Disable the current weapon, then enable the PulseRifle weapon
- Debug.Log("Equipping the PulseRifle weapon now");
- ToggleWeapon(SelectedWeapon, false);
- ToggleWeapon(2, true);
- SelectedWeapon = 2;
- }
- //Pressing 3 swaps to the RocketLauncher
- if (Input.GetKeyDown(KeyCode.Alpha3))
- {
- Debug.Log("Trying to equip the RocketLauncher.");
- //If they already have the RocketLauncher equipped do nothing
- if (SelectedWeapon == 3)
- {
- Debug.Log("RocketLauncher is already equipped.");
- return;
- }
- //If they dont have access to the RocketLauncher do nothing
- if (!WeaponManager.Instance.RocketLauncherAcquired)
- {
- Debug.Log("You dont have the RocketLauncher weapon");
- return;
- }
- //Disable the current weapon, then enable the RocketLauncher weapon
- Debug.Log("Equipping the RocketLauncher now");
- ToggleWeapon(SelectedWeapon, false);
- ToggleWeapon(3, true);
- SelectedWeapon = 3;
- }
- }
- void ToggleWeapon(int WeaponSlot, bool ShouldEnable)
- {
- switch (WeaponSlot)
- {
- //Toggle the Shotgun
- case (1):
- Shotgun.SetActive(ShouldEnable);
- break;
- //Toggle the PulseRifle
- case (2):
- PulseRifle.SetActive(ShouldEnable);
- break;
- //Toggle the RocketLauncher
- case (3):
- RocketLauncher.SetActive(ShouldEnable);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement