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 HarryMoran327 · Jun 28, 2013 at 09:53 AM · collision detectionexplosions

Help with Collision Detection for explosions?

Hey guys

I'm making a simple action where a spaceship needs to collide with an asteroid, and the code I'm using for it is here:

 var explosionPrefab : Transform;
 
 function OnCollisionEnter(collision : Collision) {
  
     var contact : ContactPoint = collision.contacts[0];
     var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal);
     var pos : Vector3 = contact.point;
     Instantiate (explosionPrefab, pos, rot); 
     Destroy (gameObject);
 }

I personally can't see anything wrong with this, as I've set both the spaceship and the object with non-kinematic RigidBodies, and Unity doesn't warn me of any scripting mistakes, but whenever I try to crash into an asteroid, the ship goes right through it, with no explosion effect. Can someone please help me with this, as I need the project to be submitted later today?enter code here

Thanks

Harry

Comment
Add comment · Show 4
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 SinisterRainbow · Jun 28, 2013 at 10:01 AM 0
Share

did you write to the debug.log if OnCollisionEnter is even executing? Do both objects have $$anonymous$$esh Colliders attached and active?

avatar image HarryMoran327 · Jun 28, 2013 at 10:11 AM 0
Share

Thanks for the suggestion, but I just did and I got an error saying "The hull has more than 255 polygons. This is invalid" Any more ideas? Thanks :)

avatar image SinisterRainbow · Jun 28, 2013 at 11:10 AM 0
Share

Just what, added a mesh collider? This code is directly from: http://docs.unity3d.com/Documentation/ScriptReference/Collision-contacts.html yes? is the oncollision enter function added to a script that is attached to the asteroid object (or whatever it is)? From what I understand, the object being hit needs a rigid body to detect contact points. Another thing, is I'm not sure why you are trying to get the contact point just to set an explosion. The explosion is likely to make the precision a bit unnecessary. This type of precision is usually reserved for things like applying a more precise force. Just set an explosion at the point where your projectile is when oncollisionenter is called and you've made everything much simpler.

avatar image bodec · Jun 28, 2013 at 12:56 PM 0
Share

on your instantiate you are calling a transform it should be a gameobject will this fix it I don't know but should help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TonyLi · Jun 28, 2013 at 01:35 PM

Are both objects on layers that are configured for collision detection? (Check in Edit > Project Settings > Physics.)

http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html

Do they both have colliders in addition to rigidbodies? The simpler the collider, the faster your game will run. Try adding a sphere collider to the asteroid and maybe a box collider to the ship.

Also, add a Debug.Log() line inside OnCollisionEnter() to make sure that the function is actually getting called.

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

18 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

Related Questions

SCRIPT NOT WORKING 2 Answers

ONGUI not working correctly, does code placement matter? 1 Answer

Whats wrong with this simple code? 2 Answers

Player Fast Travels 1 Answer

How can I record the total number of answers the player got correct and the got wrong in 2 different codes in javascript? 2 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