Advertisement
Guest User

Dovydas

a guest
May 24th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1.     public Material color;
  2.     //public GameObject hole;
  3.     public int[] bullets;
  4.     public int[] apkaboje;
  5.     public int[] talpa;
  6.     private int gunid = 0;
  7.     private bool canShot = true;
  8.     private bool isReloading = false;
  9.     public AudioClip reloadsound;
  10.     private Vector3 poz;
  11.     private Quaternion rot;
  12.     // Use this for initialization
  13.     void Start () {
  14.     }
  15.    
  16.     // Update is called once per frame
  17.     void Update () {
  18.         gunid = WeaponRotation.gunid;
  19.         RaycastHit hit;
  20.         Ray ray = Camera.main.ScreenPointToRay (new Vector3(Screen.width/2, Screen.height/2, 0));
  21.  
  22.  
  23.         if (Input.GetKeyDown (KeyCode.R)) {
  24.             if(!isReloading && apkaboje[gunid] != talpa[gunid]) Reload();
  25.         }
  26.  
  27.  
  28.         if (apkaboje [gunid] == 0 && isReloading == false) {
  29.             Reload ();
  30.         }
  31.         else {
  32.             if (Input.GetMouseButton (0) && canShot && !isReloading) {
  33.                 StartCoroutine (Wait ());
  34.                 CalculateBullets ();
  35.                 this.gameObject.transform.GetChild(0).SendMessage("PlaySound");
  36.                 if (Physics.Raycast (ray, out hit, 100)) {
  37.                     /*GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  38.                 bullet.transform.position = hit.point;
  39.                 bullet.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
  40.                 bullet.GetComponent<MeshRenderer>().material = color;*/
  41.                     //GameObject bullet = (GameObject)Instantiate(hole);
  42.                     //bullet.transform.position = hit.point;
  43.                     //hit.
  44.                     //bullet.transform.parent = hit.transform.gameObject.transform;
  45.                     if (hit.transform.gameObject.tag == "Enemy") {
  46.                         hit.transform.gameObject.SendMessage ("TakeDamage", 1);
  47.                     } else if (hit.transform.gameObject.tag == "Head") {
  48.                         hit.transform.parent.gameObject.SendMessage ("HeadShot");
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.     }
  54.  
  55.     void CalculateBullets() {
  56.         apkaboje [gunid]--;
  57.         GameObject bulltext = GameObject.Find ("Bullets");
  58.         bulltext.GetComponent<Text> ().text = apkaboje [gunid] + "/" + bullets [gunid];
  59.     }
  60.  
  61.     void UpdateBullets(int gID) {
  62.         gunid = gID;
  63.         GameObject bulltext = GameObject.Find ("Bullets");
  64.         bulltext.GetComponent<Text> ().text = apkaboje [gID] + "/" + bullets [gID];
  65.     }
  66.  
  67.     void Reload() {
  68.         if (apkaboje [gunid] < talpa[gunid]) {
  69.             int diff = talpa[gunid] - apkaboje[gunid];
  70.             if(bullets [gunid] > 0) {
  71.                 if(diff < bullets[gunid]) {
  72.                     apkaboje[gunid] = talpa[gunid];
  73.                     bullets[gunid] -= diff;
  74.                 }
  75.                 else {
  76.                     apkaboje[gunid] = bullets[gunid];
  77.                     bullets[gunid] = 0;
  78.                 }
  79.                 GameObject bulltext = GameObject.Find ("Bullets");
  80.                 bulltext.GetComponent<Text> ().text = apkaboje [gunid] + "/" + bullets [gunid];
  81.                 GameObject gun = this.gameObject.transform.GetChild(0).gameObject;
  82.                 GameObject apkaba = gun.transform.GetChild(1).gameObject;
  83.                 poz = apkaba.transform.localPosition;
  84.                 rot = apkaba.transform.localRotation;
  85.                 StartCoroutine(Reloading(apkaba));
  86.                 AudioSource.PlayClipAtPoint(reloadsound, transform.position);
  87.             }
  88.         }
  89.     }
  90.  
  91.     IEnumerator Wait() {
  92.         canShot = false;
  93.         yield return new WaitForSeconds (0.05f);
  94.         canShot = true;
  95.     }
  96.  
  97.     IEnumerator Reloading(GameObject apkaba) {
  98.         apkaba.AddComponent<Rigidbody> ();
  99.         isReloading = true;
  100.         yield return new WaitForSeconds (2f);
  101.         isReloading = false;
  102.         Destroy (apkaba.GetComponent<Rigidbody> ());
  103.         apkaba.transform.localPosition = poz;
  104.         apkaba.transform.localRotation = rot;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement