- Home /
Particles to play after an object hits the ground (Unity 5)
I have a bomb that is being dropped and I want it to disappear and an explosion particle effect (the one that is in the unity standard assets> particle systems) to come after it. I have it so when the bomb hits the ground it disappears, but I have researched and couldn't find anything that relates in the way I need it.
This is my script now (nothing really) and I would prefer it to be added onto this, but if it's not possible it's ok.
using UnityEngine; using System.Collections;
public class Destroyoncollision : MonoBehaviour {
void OnCollisionEnter(Collision otherObj) {
Destroy(otherObj.gameObject);
}
}
Answer by Cherno · Jun 13, 2015 at 08:14 PM
It's easy. You need a variable that references the prefab object you want to instantiate when the bomb explodes. In your case, this prefab would be the gameobject that holds the ParticleSystem. It's probably a good idea to destroy the explosion object after a few seconds so you scene doesn't get cluttered with invisible objects that are no longer needed.
public GameObject explosionPrefab;//Drag the particlesystem object here in the inspector
void OnCollisionEnter(Collision otherObj) {
GameObject explosionObject = Instantiate(explosionPrefab, otherObj.transform.position, Quaternion.identity) as GameObject;
explosionObject (explosionObject , 5f);
Destroy(otherObj.gameObject);
}
}
Your answer
Follow this Question
Related Questions
Freeze Gameobject's position on collision 0 Answers
Button Enabling/Disabling using Collision Triggers? 1 Answer
Multiple Cars not working 1 Answer
Not detecting collision? 1 Answer