2D Collisions/Triggers are confusing me!

Pre 4.3, I was using 3D physics for my 2D game, constraining any Z movement. It worked as expected: objects had colliders, marked as triggers or colliders, as necessary, and OnCollisionEnter and OnTriggerEnter would be called appropriately.

Post 4.3, I switched to using 2D physics (necessary because the 3D physics annoyingly allowed tunneling, despite continuous collision detection). Now, neither OnCollisionEnter2D nor OnTriggerEnter2D will be called unless the object also has a Rigidbody2D attached. Okay, fine, except for the fact that I want the objects NOT to react to physics (gravity, etc.). So I mark them as kinematic, and suddenly neither of those functions are called again. In other words, at least one of the objects has to be non-kinematic for the collisions/triggers to activate.

Is this as intended, or am I missing something? It seems strange that things would be inconsistent between 2D and 3D (regarding the Rigidbody component).

If it IS as intended, how can I work around this without writing my own collision detection (which would undoubtedly be less efficient than the native Box2D code)?

So I can get around the problem in one case, by just living with the fact that I need a non-kinematic Rigidbody. But then I have objects that NEED to be kinematic because I want to be able to move them on my own, without relying on physics. And then, of course, they won't register collisions.

could you describe in more detail about why you need certain objects to be kinematic and why certain ones to not react to physics?

Hm. So, according to this, I'm doing things correctly. http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html I'd looked at it before, but I figured I'd double check. Kinematic Rigidbody Trigger Colliders should send a trigger message. Of course, this is the 3D collider action matrix (can't find one specific to 2D!), but I don't think it's a wild assumption to assume 2D collisions should act the same. At this point, I'm starting to think it's just a bug in Unity!

Hi! I have the same problem. I think collisions should work without phisics (is object A touching object B?) but it looks like this isn't possible... And onTriggerEnter2D don't work with kinematic RigidBody2D, I think this is a bug. Personally, I have attached a RigidBody2D to "object B" with no gravity and put that in a layer that dont interact with any other layers ( in Physic2DSettings ). If someone has more information about it, I'm interested!

I'm having the same issue - I have melee weapons in 2D Toolkit that are wielded via attach points - so they have to be kinematic rigidbody2d and box2d triggers.. in 4.2 with 3d physics this worked fine but now it's completely broken.. if there's no way to get a collision without gravity physics 4.3 is going to be complete garbage..

2 Answers

2

I am not sure if this is how it is supposed to work but I tried all of the possible combinations of both 2D and 3D physics with a simple test of two objects moving in the same plane at each other using Translate(). It seems that there are two different sets of results if you use 2D or 3D. The following are my results in 4.3.4f1:

Setup:

  • Static 2D Object - Only has 2D Collider attached.
  • Kinematic 2D Object - Has 2D Collider attached and 2D rigidbody attached, IsKinematic is checked.
  • Dynamic 2D Object - Has 2D Collider attached and 2D rigidbody attached, IsKinematic is NOT checked.

Note: To prevent gravity and mass I set Gravity Scale to 0 and Mass to min (0.0001)

  • Static 3D Object - Only has 3D Collider attached.
  • Kinematic 3D Object - Has 3D Collider attached and 3D rigidbody attached, IsKinematic is checked.
  • Dynamic 3D Object - Has 3D Collider attached and 3D rigidbody attached, IsKinematic is NOT checked.

Note: To prevent gravity and mass I set Use Gravity to false and Mass to min (1e-07)

Tests and Results

When the grid says **Trigger** it indicates that I checked the trigger on the collider on that object.

[24131-2d+and+3d+physics.png|24131]

The results are the following:

  • N - No trigger or collider event fired.
  • T - The OnTriggerEnter2D() event fired in 2D and the OnTriggerEnter() event fired in 3D.
  • B - The physics engine caused a blocking so they couldn’t pass through each other. OnCollisionEnter2D() fired in 2D and OnCollisionEnter() fired in 3D.
  • T* - The OnTriggerEnter2D() event fired in 2D and the OnTriggerEnter() event fired in 3D but in 2D it fired multiple times as they passed through each other.

Differences

  • Yellow Area - Shows a difference in 2D where multiple enter events are firing if you have a dynamic pass through a static one. This I think is a bug that has been reported and I think I have seen someone is working on it.
  • Red Area - This is the big area of confusion right now it seems but the use of Box2D as part of the 2D physics engine is causing Kinematic interactions with Static and Kinematic objects to not react in 2D where they do in 3D. It seems you can kind of work around this by making one of the objects dynamic and then disabling all the mass and gravity in 2D. Hopefully this is something they plan to change in the future.

I hope this helps.

Paul

By now I believe that most of these differences are really intended by the dev team. Too late to change that now that most 2d games are working on these assumptions, too. If the 2d game needs to run away from realistic physics and use lots and lots of kinematics body, I advise to use 3d physics instead if possible (shouldn't change performance, afaik) or simply build up your own 2d collision system. I'm quite happy with a sweep sat algorithm. Learned a lot of math magic with it, too...

Awesome summary! Thanks a ton. I'd upvote the crap out of this answer but I don't have enough rep :(

This is one of the best explorations of answer that I didn't want to read :_| I really wish we could have kinematic bodies send messages back and forth for 2D physics.

This problem has been fixed in Unity 4.5 and as far as triggers and static/kinematic/dynamic goes, our 2D physics engine should behave like 3D.