Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class WeaponSwitching : MonoBehaviour
  4. {
  5.     //Tracks what weapon the player currently has equipped
  6.     public int SelectedWeapon = 1;
  7.  
  8.     //The players Pistol, RayGun and RocketLauncher weapons that will be turned on and off while switching between them
  9.     [SerializeField]
  10.     GameObject Pistol, RayGun, RocketLauncher;
  11.  
  12.     //Player starts with the pistol weapon equipped
  13.     void Start() { SwapWeapon(1); }
  14.     {
  15.         SwapWeapon(1);
  16.     }
  17.  
  18.     void Update()
  19.     {
  20.         //Pressing 1 swaps to the pistol
  21.         if(Input.GetKeyDown(KeyCode.Alpha1))
  22.         {
  23.             Debug.Log("Trying to equip the pistol");
  24.  
  25.             //If they already have the pistol equipped do nothing
  26.             if (SelectedWeapon == 1)
  27.             {
  28.                 Debug.Log("Pistol is already equipped.");
  29.                 return;
  30.             }
  31.  
  32.             //If they dont have access to the pistol do nothing
  33.             if (!WeaponManager.Instance.PistolAcquired)
  34.             {
  35.                 Debug.Log("You dont have the pistol weapon.");
  36.                 return;
  37.             }
  38.  
  39.         //Disable the current weapon, then enable the pistol weapon
  40.             Debug.Log("Equipping the pistol weapon now");
  41.             ToggleWeapon(SelectedWeapon, false);
  42.             ToggleWeapon(1, true);
  43.             SelectedWeapon = 1;
  44.         }
  45.  
  46.         //Pressing 2 swaps to the RayGun
  47.         if(Input.GetKeyDown(KeyCode.Alpha2))
  48.         {
  49.             Debug.Log("Trying to equip the RayGun");
  50.  
  51.             //If they already have the RayGun equipped do nothing
  52.             if (SelectedWeapon == 2)
  53.             {
  54.                 Debug.Log("RayGun is already equipped.");
  55.                 return;
  56.             }
  57.  
  58.             //If they dont have access to the RayGun do nothing
  59.             if (!WeaponManager.Instance.RayGunAcquired)
  60.             {
  61.                 Debug.Log("You dont have the RayGun weapon.");
  62.                 return;
  63.             }
  64.  
  65.             //Disable the current weapon, then enable the RayGun weapon
  66.             Debug.Log("Equipped the RayGun weapon now");
  67.             ToggleWeapon(SelectedWeapon, false);
  68.             ToggleWeapon(2, true);
  69.             SelectedWeapon = 2;
  70.         }
  71.  
  72.         //Pressing 3 swaps to the RocketLauncher
  73.         if(Input.GetKeyDown(KeyCode.Alpha3))
  74.         {
  75.             Debug.Log("Trying to equip the RocketLauncher.");
  76.  
  77.             //If they already have the RocketLauncher equipped do nothing
  78.             if (SelectedWeapon == 3)
  79.             {
  80.                 Debug.Log("RocketLauncher is already equipped.");
  81.                 return;
  82.             }
  83.  
  84.             //If they dont have access to the RocketLauncher do nothing
  85.             if (!WeaponManager.Instance.RocketLauncherAcquired)
  86.             {
  87.                 Debug.Log("You dont have the RocketLauncher weapon");
  88.                 return;
  89.             }
  90.  
  91.             //Disable the current weapon, then enable the RocketLauncher weapon
  92.             Debug.Log("Equipping the RocketLauncher now");
  93.             ToggleWeapon(SelectedWeapon, false);
  94.             ToggleWeapon(3, true);
  95.             SelectedWeapon = 3;
  96.         }
  97.     }
  98.  
  99.     void ToggleWeapon(int WeaponSlot, bool ShouldEnable)
  100.     {
  101.         switch(WeaponSlot)
  102.         {
  103.             //Toggle the Pistol
  104.             case (1):
  105.                 Pistol.SetActive(ShouldEnable);
  106.                 break;
  107.             //Toggle the RayGun
  108.             case (2):
  109.                 RayGun.SetActive(ShouldEnable);
  110.                 break;
  111.             //Toggle the RocketLauncher
  112.             case (3):
  113.                 RocketLauncher.SetActive(ShouldEnable);
  114.                 break;
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement