Stranar/Assets/Scripts/destroy_level_explosion.cs

82 lines
2.7 KiB
C#
Raw Normal View History

2022-06-27 11:54:53 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class destroy_level_explosion : MonoBehaviour
{
public BoxCollider box1;
public BoxCollider box2;
public BoxCollider box3;
public BoxCollider box4;
public BoxCollider box5;
public BoxCollider box6;
public BoxCollider box7;
public Rigidbody rb1;
public Rigidbody rb2;
public Rigidbody rb3;
public Rigidbody rb4;
public Rigidbody rb5;
public Rigidbody rb6;
public Rigidbody rb7;
public ParticleSystem explosion;
public Transform[] explosion_pos;
//particle systems array
BoxCollider[] box_array;
Rigidbody[] rb_array;
//audio clip for explosion
public GameObject explosion_sound;
IEnumerator Detonatetrain(){
//play explosion sound
for (int i = 0; i < explosion_pos.Length; i++)
{
//instantiate explosion at each position
GameObject asx = Instantiate(explosion_sound, explosion_pos[i].position, explosion_pos[i].rotation);
//play explosion sound
asx.GetComponent<AudioSource>().Play();
ParticleSystem explosion_clone = Instantiate(explosion, explosion_pos[i].position, explosion_pos[i].rotation) as ParticleSystem;
explosion_clone.Play();
//wait 2 seconds
yield return new WaitForSeconds(2);
//destroy explosion
Destroy(explosion_clone.gameObject);
box_array[i].enabled = false;
rb_array[i].isKinematic = false;
rb_array[i].useGravity = true;
//remove constraints
rb_array[i].constraints = RigidbodyConstraints.None;
//add random force to each box
rb_array[i].AddForce(new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), -400), ForceMode.Impulse);
}
yield return new WaitForSeconds(4);
SceneManager.LoadScene("Finish");
}
// Start is called before the first frame update
void Start()
{
rb_array = new Rigidbody[] { rb1, rb2, rb3, rb4, rb5, rb6, rb7 };
box_array = new BoxCollider[]{box1, box2, box3, box4, box5, box6, box7};
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision other) {
MeshRenderer mesh = this.GetComponent<MeshRenderer>();
BoxCollider box = this.GetComponent<BoxCollider>();
Rigidbody rb = this.GetComponent<Rigidbody>();
TrailRenderer trail = this.GetComponent<TrailRenderer>();
trail.enabled = false;
mesh.enabled = false;
box.enabled = false;
rb.isKinematic = true;
StartCoroutine(Detonatetrain());
}
}