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 nikomikulicic · Feb 28, 2014 at 12:20 PM · oncollisionentercompound colliderrigidbody 2d

Compound Colliders not working in 2D Unity?

Hello everyone!

I've got issues regarding 2D compound colliders. I tried really hard to find an answer before posting this so I hope it's not a repost.

So, I've got a scene with a Shape2D falling on a Quad, and a Shape3D falling on a Cube. This is how Shape2D and Shape3D are structured in hierarchy:

Shape2D - rigidbody2D (not kinematic), OnCollision2DScript

  • Quad - box collider 2D

  • Circle - circle collider 2D

Shape3D - rigidbody (not kinematic), OnCollision3DScript

  • Box - box collider

  • Sphere - sphere collider

My collision scripts look like this:

 void OnCollisionEnter2D(Collision2D c)
 {
     Debug.Log("Collision Enter 2D!");
 }

and similarly for 3D.

Problem: When Shape3D falls on Cube it logs "Collision Enter 3D", but when Shape2D falls on Quad, it logs nothing.

I suspect Shape3D is behaving like it should since it's a compound collider (or is it?). But what about Shape2D?

Thank you!

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 whydoidoit · Feb 28, 2014 at 12:55 PM 0
Share

Yes the 3D is right. I'm not sure on 2D collision but are you sure your colliders are the right dimensions and on the right layers for instance?

avatar image nikomikulicic · Feb 28, 2014 at 01:08 PM 0
Share

Yep... colliders are all on the same layer, and in collision matrix every layer is colliding with every other. Dimensions are also fine.

And, when I remove children of Shape2D and attach box collider 2D to it(which means i'm turning it into a regular collider ins$$anonymous$$d of compound) a "Collision Enter 2D" message is successfully displayed.

avatar image Glurth · Apr 18, 2015 at 03:34 PM 0
Share

sanity check: define the OnCollisionEnter2D functions in a script assigned to those child objects. (so THEY will be acting as regular colliders.) Do these get called?

If so, the question becomes why do they not pass the collision message to their parent like the 3d does? Of course, with this in place, you COULD do that yourself.

If not, there is definitely something wrong with those colliders, perhaps they are set to "trigger" or something like that.

avatar image nikomikulicic · Apr 18, 2015 at 04:39 PM 0
Share

I did that sanity check before asking a question (making them act like regular colliders) and messages were successfully displayed. So, you are right, the question is actually why do they not pass the collision message to their parent.

I retested this scene again in Unity5 (couple of hours before your comment) and message is now successfully passed to their parents. I also added an answer here saying that but I guess they haven't approved it yet.

2 Replies

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

Answer by nikomikulicic · Apr 18, 2015 at 08:40 PM

Now I retested this scene after upgrading to Unity 5 and seems like this problem is resolved.

Correct behavior was one of the Shape3D and now Shape2D behaves the same way. Both "Collision Enter 2D!" and "Collision Enter 3D!" messages are displayed. I guess problem with 2D was that collision message was not automatically forwarded from child object to the compound parent. I guess that was fixed in some of the in-between versions of Unity.

For earlier versions of Unity, a proper way to work around this issue would probably involve putting OnCollision2DScript on every child object that is a regular collider and adding following lines to the script:

 public class OnCollision2D : MonoBehaviour 
 {
     void OnCollisionEnter2D (Collision2D collision)
     {
         transform.parent.GetComponent<OnCollision2DCompound>().SendMessage("OnCollisionEnter2D", collision);
     }
 }

Object with rigidbody is root of the compound collider and should have OnCollision2DCompound script attached.

 public class OnCollision2DCompound : MonoBehaviour 
 {
     void OnCollisionEnter2D(Collision2D collision)
     {
         Debug.Log("Collision Enter Compound!");
     }
 }

If you don't like using SendMessage you could always specify a public function which you would call directly.

Also, nobody guarantees that immediate parent would be the root of a compound collider. It would probably be wise to iteratively search for a parent that has a rigidbody attached and get its OnCollision2DCompound script to send message to. (Or just search for an object that has OnCollision2DCompound script attached)

This should not result with reports of collisions between child objects since the compound logic actually worked in the first place. The problem was that the message was not being sent to the root of compound collider. This approach should be able to solve that.

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 DrKucho · Mar 04, 2016 at 09:18 AM

im having similar issues on 2d compound colliders

when working on unity 4.6 it was happening not all the time, but some times by bullets were colliding on the ground and enemies, rebound away , but not exploding... as the exploding was proceses in my OnCollisionEnter2D so this function was not called, the problem was happening only 5% of the times... it went away when moved to unity 5 but then, i just realized they fail to work on unit 5.2.3 but they were working on 5.1.4 , they also work on 5.3.3.

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

25 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

Related Questions

[SOLVED]Coroutine method called in OnCollisionEnter() not being called for each collision on chronological order ?! 1 Answer

Drawing Application: Changing Material of GameObject using OnCollisionEnter 0 Answers

How can I call OnCollisionEnter?, 1 Answer

When trying to find collision with with different tags, OnCollisionEnter does not differentiate if one of the to be collided entities does not have a Rigidbody? 2 Answers

Unity 2D: sprite's rigidbody weight/rotation 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