Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class bomb : MonoBehaviour {
- float detonateTime = 3f; // тэсрэх хугацаа
- public GameObject explosionBomb; // бөмбөгний prefab
- public GameObject bombRay; // тэсрэлтний prefab
- private int detonateLength = 2; // тэсрэх урт
- private RaycastHit hit; // raycast бай
- private bool isExploded = false;
- void Start () {
- if (isExploded) return;
- isExploded = true;
- StartCoroutine (destroyBomb(detonateTime));
- }
- void OnTriggerExit (Collider other) {
- if (other.tag == "Player") { // хэрвээ тоглогч байх юм бол
- gameObject.collider.isTrigger = false; // collider өгөх
- }
- }
- IEnumerator destroyBomb(float timer) {
- // дэлбэлэх
- yield return new WaitForSeconds(timer);
- Destroy (this.gameObject);
- Instantiate (explosionBomb, transform.position, Quaternion.identity);
- // raycast 4 зүг рүү хайх
- for (int i = 1; i <= detonateLength; i++) {
- // raycast зүүн тийшээ
- if (!Physics.Raycast(transform.position, Vector3.left, out hit, i)) {
- Vector3 posBombRay = new Vector3(Mathf.Round(transform.position.x - i), transform.position.y, Mathf.Round (transform.position.z));
- Instantiate(bombRay, posBombRay, Quaternion.identity);
- } else if (hit.collider.tag == "Bomb") {
- hit.collider.gameObject.SendMessage("destroyBomb", 0f);
- }
- // raycast баруун тийшээ
- if (!Physics.Raycast(transform.position, Vector3.right, out hit, i)) {
- Vector3 posBombRay = new Vector3(Mathf.Round(transform.position.x + i), transform.position.y, Mathf.Round (transform.position.z));
- Instantiate(bombRay, posBombRay, Quaternion.identity);
- } else if (hit.collider.tag == "Bomb") {
- hit.collider.gameObject.SendMessage("destroyBomb", 0f);
- }
- // raycast дээшээ
- if (!Physics.Raycast(transform.position, Vector3.forward, out hit, i)) {
- Vector3 posBombRay = new Vector3(Mathf.Round(transform.position.x), transform.position.y, Mathf.Round (transform.position.z + i));
- Instantiate(bombRay, posBombRay, Quaternion.identity);
- } else if (hit.collider.tag == "Bomb") {
- hit.collider.gameObject.SendMessage("destroyBomb", 0f);
- }
- // raycast доошоо
- if (!Physics.Raycast(transform.position, Vector3.back, out hit, i)) {
- Vector3 posBombRay = new Vector3(Mathf.Round(transform.position.x), transform.position.y, Mathf.Round (transform.position.z - i));
- Instantiate(bombRay, posBombRay, Quaternion.identity);
- } else if (hit.collider.tag == "Bomb") {
- hit.collider.gameObject.SendMessage("destroyBomb", 0f);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment