x


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?

more ▼

asked Oct 08 '11 at 08:19 PM

Dreave gravatar image

Dreave
137 82 95 96

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

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
}

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.

more ▼

answered Oct 09 '11 at 12:27 AM

aldonaletto gravatar image

aldonaletto
42.5k 16 42 202

Thanks Guys

Oct 09 '11 at 10:03 AM Dreave

oh one other thing when my grenade collides with something the particle effect has a short delay, can you help?

Oct 09 '11 at 10:18 AM Dreave

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.

Oct 09 '11 at 10:23 AM syclamoth

Oh yeah, if you use AutoDestruct on the explosion, it will delete itself- no need to use the 'Destroy(timer)' after instantiation.

Oct 09 '11 at 10:25 AM syclamoth

Ive figured it now thanks

Oct 09 '11 at 10:27 AM Dreave
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Oct 09 '11 at 02:31 AM

Jeff1N gravatar image

Jeff1N
76 1 1 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3887
x163
x10

asked: Oct 08 '11 at 08:19 PM

Seen: 2079 times

Last Updated: Oct 09 '11 at 10:27 AM