- Home /
Explosion On Collision
How would I go about writing a script which when a grenade prefab which I have, collides with anything in my world it would destroy itself and give of an explosion which I have made with a particle effect?
Answer by aldonaletto · Oct 09, 2011 at 12:27 AM
It's simple: just use OnCollisionEnter to fire the explosion:
var explosion: GameObject; // drag your explosion prefab here
function OnCollisionEnter(){
var expl = Instantiate(explosion, transform.position, Quaternion.identity);
Destroy(gameObject); // destroy the grenade
Destroy(expl, 3); // delete the explosion after 3 seconds
}
In C#:
public GameObject explosion; // drag your explosion prefab here
void OnCollisionEnter(){
GameObject expl = Instantiate(explosion, transform.position, Quaternion.identity) as GameObject;
Destroy(gameObject); // destroy the grenade
Destroy(expl, 3); // delete the explosion after 3 seconds
}
You can add the explosion sound to the explosion prefab - with Play On Awake set (is the default option) it will play automatically when the explosion is created.
oh one other thing when my grenade collides with something the particle effect has a short delay, can you help?
As in, is that what you want, or is it doing that already? If there's a delay before the explosion, check your prefab particle effect to make sure that it looks right.
Oh yeah, if you use AutoDestruct on the explosion, it will delete itself- no need to use the 'Destroy(timer)' after instantiation.
Answer by Jeff1N · Oct 09, 2011 at 02:31 AM
and if the explosion is a particle system, you can check autodestruct (and you may want to check one shot), so there's no need to destroy it manually, and you can be sure that it it run a single animation, and then it will destroy it self
I don't get it. Using a particle effect prefab from the sample assets folder and it just explodes as soon as I hit start even after using the script above on a trigger.Looking at the two scripts attached to the prefab I don't see anything on how to control it there either
Answer by HunterNacho338 · Jun 06, 2016 at 05:12 AM
That would happen if you put the particle effect prefab in the hierarchy. It should exist in the project folder only. Then it will explode only when it is instantiated by the OnCollisionEnter function. @shadowpuppet
Answer by shadowpuppet · Apr 29, 2020 at 01:29 PM
@ysimkin. Been a while since I have been in Unity, but I had a similar situation where if my motorcycle hit a wall above a certain speed it would explode, below that it would just collide. I would guess on your instantiated prefab write in some code for velocity. Looking at my scripts....
using UnityEngine;
using System.Collections;
float crashSpeed;
float CurrentSpeed;
void Update():
CurrentSpeed = transform.InverseTransformDirection(rigidbody.velocity).z;
}
if(CurrentSpeed> crashSpeed);
//explode script here, instantiate something
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Exploide GameObjects Into BrokenParts : Unity 1 Answer
Particle Effect Script Help 1 Answer
Explosion Force not working : Unity3d 1 Answer
Explosion Using Particle System 0 Answers