- Home /
Does Unity support 2D compound colliders?
I made several failed attempted to use compound collisions with BoxCollider2D and RigidBody2D. The onCollision 2D events, like onCollisionExit2D, are never called in the parent. Are 2D compound colliders supported? If so, how did you get them to work?
My parent and child tree that forms the compound collider:
GameObject
|- attached components
| |- SpriteRenderer
| |- Script
| |- RigidBody2D
| |- BoxCollider2D
|
\_ GameObject
|- attached components
|- SpriteRenderer
|- BoxCollider2D
I found several references in Unity Answers on the topic, but they either conflict or have no answer. For example, the following questions have conflicting answers and comments: http://answers.unity3d.com/questions/652188/compound-colliders-not-working-in-2d-unity.html http://answers.unity3d.com/questions/423378/how-do-you-create-complex-collision-geometry-to-ma.html
Answer by Pyrian · Jun 26, 2014 at 05:49 AM
In 4.3, collision events on the parent GameObject's script will not be called. In 4.5, they will. In either case, you can catch the events by attaching a script to the GameObject with the collider component.
How, from where, did you learn that information? I searched the docs, but after finding http://docs.unity3d.com/$$anonymous$$anual/CollidersOverview.html realized that the docs erroneous, both the online docs and the docs included in the version of Unity I have installed.
Oh, I learned it the hard way in all cases. First, in 4.3, I tried catching events at the RigidBody2D level, and when that didn't work, I added scripts to the child object colliders to catch the events and pass them up. That worked...
...Until 4.5, where the events would get called in both the parent and the child, breaking my game. So, I had to fix things again...
The change is in the 4.5 release notes:
•Physics: Collision and trigger callbacks now occur on both the GameObject containing the 2D collider as well as the GameObject containing the 2D rigid-body.