i made a trail that spawns when an object is shot , but i cant seem to make it “destroy” , basically like angry birds , when u shoot the first chicken it draws a trail , you shoot the second one , it draws another trail and destroys the previous one , any idea how to achieve such a result ? here a code snippet
public GameObject[] trails;
void OnMouseDown()
{
IsPressed = true;
rb.isKinematic = true;
}
void OnMouseUp()
{
IsPressed = false;
rb.isKinematic = false;
StartCoroutine(Release());
InvokeRepeating("spawntrail", 0.05f, 0.05f);
}
void OnCollisionEnter2D(Collision2D collision)
{
CancelInvoke("spawntrail");
}
void spawntrail()
{
if (GetComponent<Rigidbody2D>().velocity.sqrMagnitude > 10)
{
Instantiate(trails[next], transform.position, Quaternion.identity);
next = (next + 1) % trails.Length;
}
}