- Home /
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?
Well, I need them to be kinematic because I'm moving them outside the physics engine. It's my understanding that an object HAS to be kinematic if you're doing that, or the physics engine won't properly update the position, etc. Or, at least, that was the case for the 3D engine. Plus, I don't want them to react to collisions, as that would knock them around all willy nilly.
For example, I spawn "explosions" on a mouse click, and some moving object could potentially move through the explosion. I want that to happen, rather than the object being pushed back because it's hitting a Collider. Similarly, I have stationary objects that I want the moving objects to fly over without being obstructed.
Does that help paint a clearer picture?
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. $$anonymous$$inematic 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!
please don't post answers as showing the issue.
Post a Comment or Question ins$$anonymous$$d.
BTW till now I've never saw 2D physics nor OnTriggerEnter2D
it will not fire if it does not exist.
thanks for your cooperation.
Answer by pleska · Mar 23, 2014 at 09:57 PM
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.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 $$anonymous$$m. 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 ins$$anonymous$$d 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.
Answer by keely · Aug 01, 2014 at 08:20 AM
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.
Your answer
Follow this Question
Related Questions
2D 360 degress platformer example needed 0 Answers
Why Does Object Keep Getting Faster? 1 Answer
How to make a system that will let elements interact with other elements? 1 Answer
Accessing a parent's other children 1 Answer
Can I stop a lerp completely when it gets to 20% of its original value? 3 Answers