Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class weaponSwitch : MonoBehaviour {
  6.  
  7.     public int selectedWeapon = 0;
  8.  
  9. [Header("Weapon Audio Info")]
  10.     public AudioClip soundDraw;
  11.     public AudioClip soundPutaway;
  12.     public AudioSource audioSource;
  13.  
  14.     public Camera mainCamera;
  15.     public float normalFOV;
  16.  
  17.     // Use this for initialization
  18.     void Start ()
  19.     {
  20.         SelectWeapon();
  21.     }
  22.    
  23.     // Update is called once per frame
  24.     void Update ()
  25.     {
  26.         int previousSelectedWeapon = selectedWeapon;
  27.  
  28.         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
  29.            
  30.         {
  31.             if (selectedWeapon >= transform.childCount - 1)
  32.                 selectedWeapon = 0;
  33.            
  34.             else
  35.                
  36.             selectedWeapon++;
  37.         }
  38.         if (Input.GetAxis("Mouse ScrollWheel") < 0f)
  39.            
  40.         {
  41.             if (selectedWeapon <= 0)
  42.                 selectedWeapon = transform.childCount - 1;
  43.             else
  44.                
  45.             selectedWeapon--;
  46.         }
  47.         if (Input.GetKeyDown(KeyCode.Alpha1))
  48.         {
  49.             selectedWeapon = 0;
  50.         }
  51.         if (Input.GetKeyDown(KeyCode.Alpha2)&& transform.childCount >=2)
  52.         {
  53.             selectedWeapon = 1;
  54.         }
  55.  
  56.         if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
  57.         {
  58.             selectedWeapon = 2;
  59.         }
  60.         if (Input.GetKeyDown(KeyCode.Alpha4) && transform.childCount >= 4)
  61.         {
  62.             selectedWeapon = 3;
  63.         }
  64.  
  65.  
  66.         if (previousSelectedWeapon != selectedWeapon)
  67.         {
  68.             SelectWeapon();
  69.         }
  70.     }
  71.  
  72.  
  73.     void SelectWeapon()
  74.     {
  75.        
  76.         int i = 0;
  77.         foreach (Transform weapon in transform)
  78.         {
  79.             if (i == selectedWeapon)
  80.                 weapon.gameObject.SetActive(true);
  81.             else
  82.                 weapon.gameObject.SetActive(false);
  83.             i++;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement