So I have a ship, your objective is to get it from point A to point B, on it’s way to point B, I’d like to introduce some destructible objects. In fact, I’d like to absolutely litter my scene with them.
I’ve gotten to the point where I can place a prefab, have it check how hard it’s been hit, then instantiate another “destroyed” prefab and have physics do its thing.
So far so good.
That is, until I place about a dozen of these objects close together. At that point, Unity just crashes the second I hit them hard. If I hit them slowly, they just fall over without instancing the damaged object.
I’m wondering what options I have here? That pylon breaks in to about 70 voxels alone. I also don’t want to completely clean up the objects once they’ve been broken for a while. I’d like to later be able to pan a camera across the level to witness the destruction you have caused. I know at some point I’ll need to at least remove the rigidbody component from them, but what can I do until then?
I’ve included a screenshot showing the two prefabs, and the script used for destroying things.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestructionScript : MonoBehaviour
{
public GameObject destroyedVersion;
[SerializeField]public float breakEnergy;
public static float KineticEnergy(Rigidbody rb)
{
// mass in kg, velocity in meters per second, result is joules
return 0.5f * rb.mass * Mathf.Pow(rb.velocity.magnitude, 2);
}
private void OnCollisionEnter(Collision collidor)
{
if (KineticEnergy(GetComponent<Collider>().GetComponent<Rigidbody>()) >= breakEnergy)
{
Instantiate(destroyedVersion, transform.position, transform.rotation);
Destroy(gameObject);
}
}
}