Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Shoot2 : MonoBehaviour
  6. {
  7.  
  8.     public GameObject Bullet;
  9.     public GameObject Gunend;
  10.  
  11.     public float speed = 1.0F;
  12.     public float fireRate = 0.5F;
  13.     private float nextFire = 0.0F;
  14.  
  15.     float x = 1;
  16.     bool r = false;
  17.  
  18.     public AudioClip loop;
  19.     public AudioClip loopend;
  20.     public AudioClip Reload;
  21.     public AudioSource m_AudioSource;
  22.  
  23.     public Gun ThisGun;
  24.  
  25.  
  26.     // Use this for initialization
  27.     void Start()
  28.     {
  29.         ThisGun = this.gameObject.GetComponent<Gun>();
  30.     }
  31.  
  32.     // Update is called once per frame
  33.     void Update()
  34.     {
  35.         if (Input.GetKey(KeyCode.G) || Input.GetMouseButton(0) && Time.time > nextFire)
  36.         {
  37.             if (ThisGun.CurrentAmmo > 0)
  38.             {
  39.                
  40.                 GameObject go = GameObject.Instantiate(Bullet);
  41.                 ThisGun.Shoot();
  42.                 go.transform.position = Gunend.transform.position;
  43.                 go.GetComponent<Rigidbody>().AddForce(Vector3.Cross(this.transform.up, this.transform.forward) * speed, ForceMode.Impulse);
  44.                 nextFire = Time.time + fireRate;
  45.            
  46.                 m_AudioSource.clip = loop;
  47.                 m_AudioSource.loop = true;
  48.          
  49.  
  50.  
  51.                 Animator anm = GetComponent<Animator>();
  52.                 if (anm != null)
  53.                 {
  54.                     anm.Play("Shoot");
  55.                     nextFire = Time.time + fireRate;
  56.                    
  57.                 }
  58.  
  59.                 m_AudioSource.Play();
  60.  
  61.             }
  62.  
  63.             else
  64.             {
  65.              //   m_AudioSource.Play();
  66.              //   m_AudioSource.clip = loopend;
  67.             //    m_AudioSource.loop = false;
  68.                
  69.             }
  70.  
  71.         }
  72.         else
  73.         {
  74.             if (Input.GetKeyDown(KeyCode.R)) //If you press R
  75.             {
  76.                 if (ThisGun.CurrentAmmo < ThisGun.MaxAmmo()) //If your clip isn't full
  77.                 {
  78.  
  79.                     Animator anm = GetComponent<Animator>();
  80.                     if (anm != null)
  81.                     {
  82.                         anm.Play("Reload");
  83.                     }
  84.  
  85.                     x = ThisGun.RealoadingSpeed();
  86.                     r = true;
  87.                     m_AudioSource.clip = Reload;
  88.                     m_AudioSource.Play();
  89.                 }
  90.             }
  91.         }
  92.         if (r) //If you aren't already reloading AND does counter for how long the reload is
  93.         {
  94.             x -= Time.deltaTime;
  95.             Debug.Log(x);
  96.         }
  97.         if (x <= 0)
  98.         {
  99.             //Change so theres an if statement here checking if you have the ammo
  100.             ThisGun.CurrentAmmo = ThisGun.MaxAmmo();
  101.             x = 1;
  102.             r = false;
  103.         }
  104.  
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement