Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
This question was closed Apr 11, 2016 at 08:46 AM by Darth-Zoddo for the following reason:

The question is answered, right answer was accepted. problem was fixed.

avatar image
0
Question by Darth-Zoddo · Apr 06, 2016 at 09:49 AM · particlesexplosionsimplerockets

Rocket explosion goes off 'randomly' and not when i want to.

So i have made a rocket with an explosion particle attached to it. When i shoot the rocket and hits a collider, the meshrenderer is being turned off and the explosion particle is played.

When i shoot ONE rocket(when only one is in the scene), it works perfectly. However when i shoot multiple rocket in a short time, the explosions don't happen when i want to. and sometimes the explosion goes off when the rocket is mid-air and didn't hit anything.

I'm not very good in coding since i'm focussing more on art, but i think this has to do with using private vars or something(not sure)?

If anyone could help me out, that'll be great. The script that is attached to my rocket is down below.

 var time : float;
 var mesh : MeshRenderer;
 
 function Start (){
 gameObject.Find("SmokeTrail").GetComponent(ParticleSystem).enableEmission = true;
 }
 
 function OnCollisionEnter(theCollision : Collision){
         Destroy(gameObject,time);
         mesh.enabled = false;
         Emit = true;
         gameObject.Find("SmokeTrail").GetComponent(ParticleSystem).enableEmission = false;
         gameObject.Find("Explosion").GetComponent(ParticleSystem).Play();
         Destroy(gameObject,time);
 
     }
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Fredex8 · Apr 07, 2016 at 01:47 PM

Firstly it's a really bad idea to be using gameObject.Find within OnCollisionEnter. The one you are using on Start is far from ideal but the time it takes to find objects would be enough to delay the particle effects visibly on collision when you have multiple rockets on screen. No guarantee with this that they will actually find the explosion object on themselves and not on another rocket instead.

Also not clear why you have Destroy(gameObject,time); in there twice. Seems like that is either going to be redundant or potentially call a null reference when the second destroy cannot find the item since it has already been destroyed (if it wasn't destroying itself it would anyway). If you want the rocket to destroy whatever it hit use Destroy(theCollision.gameObject). I also cannot see where you have actually set the value of time. If unset it will be 0 by default and therefore be destroying things instantly.

There are a few other ways to do this. First is for each rocket item to have SmokeTrail and Explosion on them as a particle system and assigned to variables in the inspector so you don't need to mess around with Find or GetComponent and can just enable/disable them directly. Or they could be child objects within the rocket prefab which would work much the same. That would work fine for the SmokeTrail.

For the explosion however I would probably look at instantiating a prefab with the explosion particle system on it at the point the rocket collided. Use Destroy(explosionInstance, 1); (or however long the particle animation is) to destroy the instantiated explosion on a timer or a script on the explosion prefab to do it. After instantiating the explosion destroy the rocket itself instantly rather than turning off the renderer and smoke trail.

EDIT:

 //Assign particles in the inspector instead of with Find
 //In the particle editor set them to Play on Awake
 
 //Child object of rocket with the trail particles on
 var rocketTrail : GameObject;
 
 //Not a child object but a prefab from your scene
 var explosion : GameObject;
 
 function OnCollisionEnter(theCollision : Collision){
        
     //Spawn explosion
     var explosionInstance : GameObject = Instantiate(explosion, transform.position, transform.rotation);    
     Destroy(explosionInstance, 2f);
     
     //Destroy rocket
     Destroy(gameObject);         
 }

My JS is a bit rusty so you may need to check that but this should remove the need to use Find which is slowing things down for you. Remove the explosion from the rocket itself and just have it assigned as a prefab from your project. The reference to rocketTrail above isn't doing anything currently but you may want it to stop emitting particles if the rocket has a limited range and is going to run out of fuel or something. Either way avoid using Find at all costs and assign things in the inspector where you can.

Comment
Add comment · Show 3 · 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 Darth-Zoddo · Apr 08, 2016 at 09:48 AM 0
Share

thanks for the reply!

i have no idea why i have Destroy(gameobject,time); twice in this.

The rocket itself is ofcourse a prefab and in the inspector i set the tim to 2 secs. The rocket prefab also has got children yes and those are the smoketrail and explosion particles.

So if I get this right, you're telling me to let the rocket be destroyed on impact. And to spawn in the explosion on the rocket's location when it impacts?

Any idea how I can make the Explosion spawn and be destroyed within the this script with Destroy(explosionInstance);?

avatar image Fredex8 Darth-Zoddo · Apr 08, 2016 at 01:40 PM 0
Share

Provided destroying the rocket is the last line of code after everything you want to rocket to do on collision: spawn the explosion, apply damage or force to enemies, destroy objects etc then yes it is better than turning off the renderer on it.

I added some code to the answer above which should work for you.

avatar image Darth-Zoddo Fredex8 · Apr 11, 2016 at 08:45 AM 0
Share

I fixed it by adding a collider to the explosion particle. And I made it so that it explodes when it comes in contact with another collider.

it's a newbie mistake i made, by trying to merge multiple scripts with eachother. $$anonymous$$y script got kind of messed up by doing that. It's fixed now.

thanks for replying to my question atleast!!

Follow this Question

Answers Answers and Comments

57 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemies drop coins 1 Answer

Just Cause 3 Volumetric Explosions 1 Answer

how to activate a particle when bullet hits a collider? 1 Answer

Particle Collision causes Explosion?,Explosion on particle contact? 0 Answers

Need some assistance with spot/point lighting for GPU based particles 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