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