Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dreave · Oct 08, 2011 at 08:19 PM · explosionparticleeffect

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dreave · Oct 09, 2011 at 10:03 AM 0
Share

Thanks Guys

avatar image Dreave · Oct 09, 2011 at 10:18 AM 0
Share

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

avatar image syclamoth · Oct 09, 2011 at 10:23 AM 0
Share

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.

avatar image syclamoth · Oct 09, 2011 at 10:25 AM 0
Share

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

avatar image Dreave · Oct 09, 2011 at 10:27 AM 0
Share

Ive figured it now thanks

Show more comments
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shadowpuppet · Jun 05, 2016 at 11:15 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
     }
     }
 



 
 



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges