Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class GunControl : MonoBehaviour {
  6.  
  7.     public GameObject bulletPrefab;
  8.     public Transform pointShot;
  9.     public float delay;
  10.     public float powerShots = 300f;
  11.     public Button buttonShot;
  12.     public float RechargeTime = 5;
  13.  
  14.     public Slider sliderY;
  15.     public Slider sliderX;
  16.  
  17.     private float maxRechargeTime;
  18.     private bool timerBool = false;
  19.     private bool delayBeforeFiring = false;
  20.  
  21.     //sound
  22.     public AudioClip soundFire;
  23.     public AudioClip soundRecharge;
  24.     //sound
  25.  
  26.     void Start () {
  27.         maxRechargeTime = RechargeTime;
  28.     }
  29.  
  30.     void Update () {
  31.  
  32.         Timer();
  33.         Delay();
  34.    
  35.     }
  36.  
  37.  
  38.     void Delay () {
  39.         if(delayBeforeFiring){
  40.             buttonShot.interactable = false;
  41.             sliderX.interactable = false;
  42.             sliderY.interactable = false;
  43.  
  44.             if(audio.clip == null)
  45.             audio.PlayOneShot(soundRecharge);
  46.         }
  47.         else
  48.         {
  49.             buttonShot.interactable = true;
  50.             sliderX.interactable = true;
  51.             sliderY.interactable = true;
  52.             audio.Stop();
  53.         }
  54.     }
  55.  
  56.     void Timer () {
  57.  
  58.         if(RechargeTime >= 0 && timerBool){
  59.             RechargeTime -= Time.deltaTime;
  60.         }
  61.  
  62.         if(RechargeTime <= 0) {
  63.             delayBeforeFiring = false;
  64.             timerBool = false;
  65.             RechargeTime = maxRechargeTime;
  66.         }
  67.     }
  68.  
  69.     public void ShotButton () {
  70.  
  71.         Invoke("Shot", delay);
  72.    
  73.     }
  74.  
  75.     void Shot() {
  76.         RaycastHit hit;
  77.         Ray ray = new Ray(pointShot.localPosition, -Vector3.forward);
  78.  
  79.         timerBool = true;
  80.         audio.PlayOneShot(soundFire);
  81.         if(!delayBeforeFiring){
  82.             if (Physics.Raycast(ray, out hit))
  83.                 Instantiate(bulletPrefab, hit.point, transform.rotation);
  84.    
  85.         }
  86.         delayBeforeFiring = true;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement