- Home /
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!
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?
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.
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.
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.
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.
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.