Hi! I want to shatter a 2d object, I am looking for options and most of them using shattered prefabs with rigidbodys. But I can’t use rigidbody because my game is top-down so there is no collider under the object and i can’t use gravity.
Is there other options to shatter a 2d object without using rigidbody?
Here is an example script that splits an object into pieces when hitting an object tagged “Bullet”:
using UnityEngine;
public class ShatterGameObject : MonoBehaviour
{
public GameObject shatteredGameObject;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Bullet"))
return;
var shatteredObj = Instantiate(shatteredGameObject, transform.position, transform.rotation);
var rigidbodies = shatteredObj.GetComponentsInChildren<Rigidbody2D>();
foreach (Rigidbody2D rb2d in rigidbodies)
{
Vector2 force = rb2d.transform.position - collision.transform.position;
rb2d.AddForce(force.normalized * 100);
}
Destroy(gameObject);
}
}
_
Making a simple scene I got the following result (GIF):

_
Main Object and Shards parameters (GIF):

(Note: I have increased the LinearDrag value to slow down the shards over time).