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 HumanError · Nov 30, 2012 at 04:52 PM · collisiontriggerexplode

Destroy On Collision

I got a rocket that follow you until it collide with something and explode. I wrote a simple code for the collision. The problem is that the rocket don't explode when it should: it fly through walls , touch me but continue to fly around, it explode only when I shoot on it.

The code I wrote shoulde make it explode when it collide with anything that have a collider, trigger or not:

 var explodPrefab : Transform;
 var spawnPoint : Transform;
 
 function OnCollisionEnter (hit : Collision)
 {
    var exp = Instantiate(explodPrefab, spawnPoint.transform.position, Quaternion.identity);
    Destroy(gameObject);
 }
 
 function OnTriggerEnter (hit : Collider)
 {
    var exp = Instantiate(explodPrefab, spawnPoint.transform.position, Quaternion.identity);
    Destroy(gameObject);
 }
 

Note: all the game objects have a collider on them.

Why this is happening?

Comment
Add comment · Show 3
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 DeveshPandey · Dec 01, 2012 at 10:59 AM 0
Share

collision will detect only for those body which have collider, e.g if "A" is colliding with "B" then there must be a collider with both A & Bm have don this? Note: A & B are game objects.

avatar image HumanError · Dec 01, 2012 at 03:25 PM 0
Share

All the game objects have a collider on them.

avatar image HumanError · Dec 05, 2012 at 03:05 PM 0
Share

Up. I need an nanswer for this question.

4 Replies

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

Answer by uvavoo · Dec 05, 2012 at 08:14 PM

Try the script from the first person shooter tutorial. Script also assumes you have a particle emitter attached for the rocket trail. I use this and it works perfectly.

 // The reference to the explosion prefab
 var explosion : GameObject;
 var timeOut = 3.0;
 
 // Kill the rocket after a while automatically
 function Start () {
     Invoke("Kill", timeOut);
 }
 
 
 function OnCollisionEnter (collision : Collision) {
     // Instantiate explosion at the impact point and rotate the explosion
     // so that the y-axis faces along the surface normal
     var contact : ContactPoint = collision.contacts[0];
     var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
     Instantiate (explosion, contact.point, rotation);
 
     // And kill our selves
     Kill ();    
 }
 
 function Kill () {
     // Stop emitting particles in any children
     var emitter : ParticleEmitter= GetComponentInChildren(ParticleEmitter);
     if (emitter)
         emitter.emit = false;
         
 
     // Detach children - We do this to detach the trail rendererer which should be set up to auto destruct
     transform.DetachChildren();
         
     // Destroy the projectile
     Destroy(gameObject);
     
 }
 
 
 //@script RequireComponent (Rigidbody)
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 HumanError · Dec 07, 2012 at 05:47 AM 0
Share

Thank you, it works now.

avatar image
0

Answer by kaolas · Nov 30, 2012 at 06:29 PM

Based on your description, I don't think your collision detection is working correctly. Do both your rocket and the object it is colliding with have colliders on them? Both will need a collider for a collision to be detected.

Comment
Add comment · Show 2 · 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 HumanError · Dec 01, 2012 at 09:58 AM 0
Share

Yeah, the rocket need to destroy itself when it collide with anything that have a collider on it, even if the collider is a trigger. The rocket have a collider on it, it is not a trigger and the objects it need to collide with have a collider as well. The problem can be seen mostly when it go through the terrain.

avatar image kaolas · Dec 05, 2012 at 07:56 PM 0
Share

Does the explosion prefab/effect get created successfully? If not, your hit code probably isn't being triggered. Your code in the question also says that the explodPrefab is a Transform. You can't instantiate a transform. It needs to be a GameObject. You may be getting an error when trying to instantiate the Transform and then it doesn't execute the Destroy()

avatar image
0

Answer by neogrant2 · Dec 01, 2012 at 05:19 PM

Replace it with

 Destroy(hit.gameObject);
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 HumanError · Dec 03, 2012 at 03:23 PM 0
Share

But this will destroy the object the rocket hit and not the rocket...

I am using this code on the rocket, and the rocket should explode whenever it hit something with a collider, trigger or not.

avatar image
0

Answer by uvavoo · Dec 05, 2012 at 08:14 PM

I read somewhere that for fast moving objects the collider will pass through objects without triggering a collision (ie gets past object between 'updates'). Try making the collider bigger or slowing down the rocket.

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

15 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

Related Questions

How to add to a 3D object's length through code? 1 Answer

Overlap point of 2 kinematic colliders 1 Answer

How to add the right amount of points when my player destroys a gameobject? 0 Answers

Receiving information about getting hit by a Raycast 2 Answers

How do you execute Trigger-collider collision only in one gameobject? 1 Answer


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