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 Ranom · Nov 14, 2012 at 07:49 PM · collisionrigidbodyoncollisionenter

Getting OnCollisionEnter to fire on composite object

Question

Given the following hierarchy of objects/components:

Parent Object       : rigidbody
 |_ Child Object 1  : mesh filter, mesh renderer, mesh collider
 |_ Child Object 2  : mesh filter, mesh renderer, mesh collider

I want to detect when one of the child objects is hit. I've created a basic collision detection script using the OnCollisionEnter callback however the collision event callback is not fired.

     public void OnCollisionEnter(Collision collision)
     {    
         Debug.Log("Collision detected on " + gameObject.name);
     }

  • Investigation **

If I take the child object and add a rigidbody without changing anything else, this works fine:

Parent Object       : rigidbody
 |_ Child Object 1  : mesh filter, mesh renderer, mesh collider, rigidbody
 |_ Child Object 2  : mesh filter, mesh renderer, mesh collider, rigidbody

However this gives the incorrect behaviour (it's no longer a composite rigidbody) and I want to avoid having to join all the child objects together with constraints.

In the desired hierarchy, detecting collision on the projectile works fine. I'm guessing the problem is something to do with the mesh collider and rigidbody being on different objects.

The documentation states:

A Collider does not necessarily need a rigidbody attached, but a rigidbody must be attached in order for the object to move as a result of collisions.

And the object does indeed move after it has been hit correctly, I just don't get the collision event.

Any help greatly appreciated!

Comment
Add comment · Show 6
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 whydoidoit · Nov 14, 2012 at 09:18 PM 0
Share

You're right, I'd expect that to work especially if the object is moving correctly ($$anonymous$$eshColliders can be a nightmare) - I'm guessing that your projectile is a primitive?

avatar image whydoidoit · Nov 14, 2012 at 09:19 PM 0
Share

Oh hang on - you are checking for the collision on the parent right? You only get the collision on the thing that has the rigidbody. (Thinking you could possibly add a kinematic rigidbody to the children or you are going to have to do some more work...).

avatar image cassius · Nov 14, 2012 at 09:48 PM 0
Share

If I recall correctly, when using $$anonymous$$esh Colliders you must also include a rigidbody... or a least a rigidbody on one of the two items colliding. I could be wrong about that but seem to remember reading it.

avatar image Ranom · Nov 14, 2012 at 11:43 PM 0
Share

Thanks for your responses!

whydoidoit: I was checking for the collision on the child object. I guess looking at the Collision object I could go collision.collider.gameobject to get the child. I'll try adding the script to the parent and see what I get.

cassius: the composite object has a rigidbody on the parent gameobject, the projectile is a single gameobject with collider and rigidbody (the event does fire on this one).

avatar image kromenak · Nov 14, 2012 at 11:45 PM 1
Share

You may want to consider using a more primitive collider and seeing if that simplifies things at all. $$anonymous$$esh colliders are pretty heavy performance-wise, and there is added complexity as a result.

Show more comments

2 Replies

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

Answer by Ranom · Nov 21, 2012 at 11:18 AM

whydoidoit: you're spot on:

  1. Move the collision script to the parent object.

  2. collision.contacts[0].thisCollider.gameObject returns the child gameObject in the hierarchy.

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

Answer by sparkzbarca · Nov 15, 2012 at 06:34 AM

judging by the info on compound colliders here.

http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html

compound colliders cant be made up of multiple mesh colliders.

only a collection of primitive colliders.

Mark as answered please. :)

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 whydoidoit · Nov 15, 2012 at 07:47 AM 1
Share

That is wrong - you can use multiple meshes as compound colliders.

If you read the question again - you'll see he has the right physical effect already (meaning his colliders are already working in PhysX)- as I say in the comments above, the problem is checking for the collision on the child when it will in fact be reported on the parent.

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

13 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

Related Questions

CharacterController and Normal rigidbody Box Collider Collision issues 0 Answers

OnCollisionEnter and rigidbody problem 1 Answer

OnCollisionEnter not working 3 Answers

onCollisionEnter doesn't work without RIgidbody? 1 Answer

Two Character Controllers Colliding... 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