- Home /
Question by
GameDevH2O · Jun 17, 2018 at 10:16 AM ·
c#2dmobile
How to make object explode upon collision
I have box colliders in every object & I need when an object collides with another for it to explode. I already have a explosion prefab. How do I make the explosion happen when on collides with another?
Comment
Answer by phxvyper · Jun 17, 2018 at 10:38 AM
Use the OnCollisionEnter(Collision) Unity event. Which will be triggered whenever another collider that fits some parameters enters or touches our collider. See this page for more details on which colliders interact with other colliders.
// will show up in inspector, attach the prefab to this field.
[SerializeField] private GameObject _explosionPrefab;
void OnCollisionEnter(Collision collision)
{
// instace the prefab at our position and rotation
Instantiate(_explosionPrefab, transform.position, transform.rotation);
// then destroy the game object that this component is attached to
Destroy(gameObject);
}
I dragged the prefab into the field & for some reason the explosion prefab did not show up the objects just disappeared
Your answer
Follow this Question
Related Questions
New Input System, Multiple controllers plugged in Issue. 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers