In my game I’d like for a wall to explode next to my character when he passes through an invisible box collision trigger next to the wall. I have a solid box prefab which spawns broken pieces that will be affected by gravity through a Rigidbody component.
My problem is that I can’t connect the trigger to the rest of the code. I admit I’m new to C# so I know I’m probably missing something, but I’ve been sitting here for about half an hour with little results. I’m building the code off of quill18create’s destructible walls tutorial.
TL;DR I need to create an invisible trigger which, when passed through, will replace my solid box with the destructible one.
Here’s what I have so far:
using UnityEngine;
using System.Collections;
public class Destructable : MonoBehaviour {
public GameObject debrisPrefab;
public Collider debrisTrigger;
void OnCollisionEnter (Collision col){
if(col.gameObject.name == "collapseTrigger")
{
Destroy(col.gameObject);
DestroyMe();
}
}
void DestroyMe()
{
if(debrisPrefab)
{
Instantiate(debrisPrefab, transform.position, transform.rotation);
}
Destroy(gameObject);
}
}