thisizmonster

Untitled

Nov 24th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class bomb : MonoBehaviour {
  5.    
  6.     float detonateTime = 3f;             // тэсрэх хугацаа
  7.     public GameObject explosionBomb;     // бөмбөгний prefab
  8.     public GameObject bombRay;           // тэсрэлтний prefab
  9.     private int detonateLength = 2;      // тэсрэх урт
  10.     private RaycastHit hit;              // raycast бай
  11.     private bool isExploded = false;
  12.  
  13.     void Start () {
  14.  
  15.         StartCoroutine (destroyBomb(detonateTime));
  16.     }
  17.    
  18.     void OnTriggerExit (Collider other) {
  19.  
  20.         if (other.tag == "Player") {    // хэрвээ тоглогч байх юм бол
  21.  
  22.             gameObject.collider.isTrigger = false;   // collider өгөх
  23.         }
  24.     }
  25.    
  26.     IEnumerator destroyBomb(float timer) {
  27.  
  28.         If (!isExploded) {
  29.  
  30.             isExploded = true;
  31.             // дэлбэлэх
  32.  
  33.             yield return new WaitForSeconds(timer);
  34.             Destroy (this.gameObject);
  35.             Instantiate (explosionBomb, transform.position, Quaternion.identity);
  36.  
  37.             // raycast 4 зүг рүү хайх
  38.  
  39.             for (int i = 1; i <= detonateLength; i++) {
  40.  
  41.                 // raycast зүүн тийшээ
  42.                 if (!Physics.Raycast(transform.position, Vector3.left, out hit, i)) {
  43.  
  44.                     Vector3 posBombRay = new Vector3(Mathf.Round(transform.position.x - i), transform.position.y, Mathf.Round (transform.position.z));
  45.                     Instantiate(bombRay, posBombRay, Quaternion.identity);
  46.                 } else if (hit.collider.tag == "Bomb") {
  47.  
  48.                     hit.collider.gameObject.SendMessage("destroyBomb", 0f);
  49.                 }
  50.  
  51.                 // raycast баруун тийшээ
  52.                 if (!Physics.Raycast(transform.position, Vector3.right, out hit, i)) {
  53.                    
  54.                     Vector3 posBombRay = new Vector3(Mathf.Round(transform.position.x + i), transform.position.y, Mathf.Round (transform.position.z));
  55.                     Instantiate(bombRay, posBombRay, Quaternion.identity);
  56.                 } else if (hit.collider.tag == "Bomb") {
  57.                    
  58.                     hit.collider.gameObject.SendMessage("destroyBomb", 0f);
  59.                 }
  60.  
  61.                 // raycast дээшээ
  62.                 if (!Physics.Raycast(transform.position, Vector3.forward, out hit, i)) {
  63.                    
  64.                     Vector3 posBombRay = new Vector3(Mathf.Round(transform.position.x), transform.position.y, Mathf.Round (transform.position.z + i));
  65.                     Instantiate(bombRay, posBombRay, Quaternion.identity);
  66.                 } else if (hit.collider.tag == "Bomb") {
  67.                    
  68.                     hit.collider.gameObject.SendMessage("destroyBomb", 0f);
  69.                 }
  70.  
  71.                 // raycast доошоо
  72.                 if (!Physics.Raycast(transform.position, Vector3.back, out hit, i)) {
  73.                    
  74.                     Vector3 posBombRay = new Vector3(Mathf.Round(transform.position.x), transform.position.y, Mathf.Round (transform.position.z - i));
  75.                     Instantiate(bombRay, posBombRay, Quaternion.identity);
  76.                 } else if (hit.collider.tag == "Bomb") {
  77.                    
  78.                     hit.collider.gameObject.SendMessage("destroyBomb", 0f);
  79.                 }
  80.             }
  81.  
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment