thisizmonster

Untitled

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