Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public enum Weapon{
  6. Police9mm,
  7. PortableMagnum,
  8. }
  9.  
  10. public class WeaponManager : MonoBehaviour {
  11. public static WeaponManager instance;
  12. private int currentWeaponIndex = 0;
  13. private Weapon[] weapons = { Weapon.Police9mm, Weapon.Police9mm };
  14.  
  15. void Awake(){
  16. if(instance == null){
  17. instance = this;
  18. }
  19. else{
  20. Destroy(gameObject);
  21. }
  22. }
  23.  
  24. void Start(){
  25. SwitchTocurrentWeapon();
  26. }
  27.  
  28. void Update(){
  29. CheckWeaponSwitch();
  30. }
  31.  
  32. void SwitchTocurrentWeapon(){
  33. for(int i = 0; i < transform.childCount; i++){
  34. transform.GetChild(i).gameObject.SetActive(false);
  35. }
  36. transform.Find(weapons[currentWeaponIndex].ToString()).gameObject.SetActive(true);
  37. }
  38.  
  39. void CheckWeaponSwitch(){
  40. float mousewheel = Input.GetAxis("Mouse ScrollWheel");
  41.  
  42. // Forward
  43. if(mousewheel > 0){
  44. SelectPreviousWeapon();
  45. }
  46. // Backward
  47. else if(mousewheel < 0){
  48. SelectNextWeapon();
  49. }
  50. }
  51.  
  52. void SelectPreviousWeapon() {
  53. if(currentWeaponIndex == 0) {
  54. currentWeaponIndex = weapons.Length - 1;
  55. }
  56. else {
  57. currentWeaponIndex++;
  58. }
  59.  
  60. SwitchTocurrentWeapon();
  61. }
  62.  
  63. void SelectNextWeapon() {
  64. if(currentWeaponIndex >= (weapons.Length - 1)) {
  65. currentWeaponIndex = 0;
  66. }
  67. else {
  68. currentWeaponIndex++;
  69. }
  70.  
  71. SwitchTocurrentWeapon();
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement