Files

51 lines
1.2 KiB
C#
Raw Permalink Normal View History

2025-05-13 05:19:58 +03:00
using System.Linq;
using UnityEngine;
using System.Collections;
[SelectionBase]
public class BreakableObject : MonoBehaviour {
public Health health;
public RandomSoundPlayer player;
public GameObject[] instantinateOnDestroy;
// Use this for initialization
void Start () {
if (health != null) {
health.Destroyed += health_Destroyed;
}
}
void health_Destroyed() {
Break();
}
public void Break() {
var thisRb = GetComponent<Rigidbody2D>();
var children = gameObject.GetComponentsInChildren<BreakablePart>();
foreach (var child in children) {
var rb = child.gameObject.AddComponent<Rigidbody2D>();
rb.mass = thisRb.mass/children.Count();
child.transform.parent = null;
}
if (player != null) {
player.Play(true);
}
if (instantinateOnDestroy != null && instantinateOnDestroy.Length > 0) {
foreach (var o in instantinateOnDestroy) {
var item = Instantiate(o);
item.SetActive(true);
item.transform.parent = null;
}
}
Destroy(gameObject);
}
// Update is called once per frame
void Update () {
}
}