Advertisement
Guest User

Untitled

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