Search Unity

2D Physics: Collisions/Triggers

Discussion in '2D' started by Togglesworlh, Nov 17, 2013.

  1. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Hello!

    The summary: collisions and triggers are not working as I expected. (I've posted on Unity Answers, but no answer, yet, so I figured I'd try here!)

    So, in 3D, pre-4.2, I had an object that only had a BoxCollider. I click to spawn an explosion object that had a Kinematic Rigidbody and a SphereCollider, which expands in radius with an animation. Both marked as triggers. OnTriggerEnter is correctly called on both objects.

    I made the switch to use 2D physics with 4.3. I have an object that only has a BoxCollider2D, and I spawn objects that have a Kinematic Rigidbody2D and a CircleCollider2D, which again expand with an animation. Both marked as triggers. OnTriggerEnter2D is never called.

    After some experimenting, I found that the only way I could get OnTriggerEnter2D to execute is if I DON'T set that Rigidbody2D as kinematic. The same holds true for OnCollisionEnter2D when the objects aren't triggers.

    As I said on that Unity Answers post, I went over the table at the bottom of this page again, and, unless there's an intended difference between 3D and 2D physics, it should work as I want it to.

    Is this a bug in Unity, or am I going insane?

    Thanks!
     
  2. altheonix

    altheonix

    Joined:
    Oct 20, 2012
    Posts:
    19
    I have same problem too.

    After changed all 3D physics collider and rigidbody to 2D. ontrigger/oncollision don't work.
    It only happen on kinematic rigidbody.
    I run a test scene testing both 3D and 2D trigger with kinematic rigidbody. only 3D trigger work.

    Here's the test scene
     

    Attached Files:

  3. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Well, at least I'm not the only one having the problem! That sucks. Must be a bug in Unity after all.
     
  4. 1HpLeft

    1HpLeft

    Joined:
    Dec 13, 2012
    Posts:
    81
    I have the same problem...I think I'm gonna use 3D collider ( if that's work ) and I'll switch to 2D collider when the bug will be fixed...
     
  5. rinigado

    rinigado

    Joined:
    Nov 17, 2013
    Posts:
    3
    likewise, the only thing working that I can see at the moment is use 3D collision, or don't use 2D kinematic rigid bodies. Hopefully they're working on a fix for kinematic 2D collisions!
     
  6. MisterBiscuit

    MisterBiscuit

    Joined:
    Feb 18, 2013
    Posts:
    14
    Just a question, are you trying to get 2 kinematic rigid bodies to collide with each other? If so, it can't be done AFAIK. I am not an expert by any means though. If this isn't what you were trying to do, then completely ignore this response.
     
  7. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    No, I'm trying to get two Kinematic Rigidbody Trigger Colliders to call the OnTriggerEnter2D function.

    Or, rather, a Kinematic Rigidbody Trigger Collider hitting any trigger at all, it just won't work. It has to be non-kinematic. Which is counter to what's in this table at the bottom of this page: http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
     
  8. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
    Confirmed, neither OnCollision2D nor OnTrigger2D are being called between Kinematic Rigidbody2Ds.

    It works with 3D physics; 2D is supposed to provide identical, or at least analogous behaviour.
     
  9. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Actually, he's right, Kinematic Rigidbody Colliders (as in, non-Triggers) aren't supposed to collide. The page I linked above (I'll just link it again: http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html) shows which collisions/triggers are supposed to work.

    If it does work in 3D, it's not actually supposed to. Unless that page is out of date, I suppose, which is entirely possible!
     
  10. rinigado

    rinigado

    Joined:
    Nov 17, 2013
    Posts:
    3
    the place it seems to fall down for me, is detecting trigger events when a kinematic rigid body intersects any trigger collider in the scene, or trigger events when a kinematic trigger rigid body intersects with any collider. Both off these cases are currently not working in 2D for me.
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Colliders on Kinematic bodies can only collide with colliders on Dynamic bodies. It doesn't matter whether the collider is set to be a trigger or not. This is determined by Box2D itself.

    Box2D imposes the following valid body-type collisions:

    Static with Dynamic
    Dynamic with Static, Dynamic Kinematic
    Kinematic with Dynamic

    Hope this helps.
     
    PrisedRabbit likes this.
  12. rinigado

    rinigado

    Joined:
    Nov 17, 2013
    Posts:
    3
    It's been a while since I looked at box2d directly, but can a sensor (i.e. isTrigger) intersection with static colliders not be detected?
     
  13. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Yeah it's triggers (or sensors, apparently!) that I'm most interested. Still, thanks for the info, guess it's a limitation of Box2D. Don't know why I didn't consider that. Oh well!
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Technically there is no such thing as a "trigger", it's obviously just a collider that is in a trigger "mode" so the same rules apply. The only difference being whether it actually causes forces to be applied to the colliding objects, how it is reported in callbacks and if it constitutes mass to the rigid-body.

    Unfortunately, it would be difficult to change this behaviour in Box2D.
     
  15. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Understood!

    (As an aside, every single one of your posts, across multiple threads, appears to me as a giant empty post with tons of white space. I have to reply with quote to even see what you're saying.)
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    That's a little crazy, I'm not sure why that is as I just hit reply and type some text. :confused:
     
  17. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Yeah I'm not too sure! I'm like 99% certain it's not your fault. I noticed it with a different Unity developer's post, too. It must be a weird bug with Chrome or some extension I have. Although I don't think I have any that would affect these forums, so... Yeah I'm not too sure!
     
  18. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Is that the same legendary melv may from Torque2D ? I was doing a match 3 ancient era game on that once, seemed like a very nice chap :)
     
  19. Gredlen

    Gredlen

    Joined:
    Nov 26, 2013
    Posts:
    1
    I'm having this problem in Safari on my iPhone. Weirdly enough, it was only after I logged in; I could see them fine before logging in.

    Anyway, I've been having problems with triggers myself and I just wanted to clarify things:
    Two 3D kinematic rigidbodies won't send collide messages, but they WILL send trigger messages.
    Two 2D kinematic rigidbodies won't send trigger messages, either.

    Is the fact that the two function differently by design or a bug?
     
  20. ryinmo

    ryinmo

    Joined:
    Mar 19, 2013
    Posts:
    2
  21. Aurore

    Aurore

    Director of Real-Time Learning

    Joined:
    Aug 1, 2012
    Posts:
    3,106
    I think I've properly fixed your issue now, it even has a correct title now too
     
  22. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Yeah it's fixed now! Thanks!
     
  23. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Real question time again!

    Am I blind? I don't see any option to make a Rigidbody2D to static, as advertised on the 4.3 feature update list
    or like MelvMay was talking about earlier in the thread.

    Kinematic vs Dynamic is pretty obvious. Check or uncheck the box. But static? As of right now, I can't for the life of me figure out how to make an unmovable object.
     
  24. meta87

    meta87

    Joined:
    Dec 31, 2012
    Posts:
    254
    I could be mistaken, but I think static means it has no rigidbody2d component, just a 2d collider.
     
  25. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Hm, I suppose that's about right, yeah.

    My problem (which I just realized wouldn't be solved by a "static rigidbody", anyway) is that I want a kinematic object to collide with a wall. A wall should obviously be static, or unmovable. I also want this wall to be an actual wall for other, dynamic objects.

    So, let's say I've got a ship that is kinematic that I move around all on my own. I want it to hit the wall, detect the collision, and bounce off. It's kinematic because I also want it NOT to react to collisions from other objects that exist in the scene (such as other ships, or the ball, as described below). So I need the wall to be dynamic, because that's the only way I'll detect any collisions between ship and wall.

    And then I've got a ball that gets knocked around by explosions and such. I want it to just be part of the physics simulation. Which means I need the walls to be static, because otherwise they get nudged around little by little.

    I can set the ball mass to be as small as possible and the wall mass to be as large as possible, but that's considerably less than ideal, right? How do I make an unmovable object that is still dynamic? Defeats the purpose of making it unmovable if it's dynamic, but it has to be for the kinematic bodies! Dang.
     
    Last edited: Dec 5, 2013
  26. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Keep in mind that this kind of problem is a Box2D problem, not a Unity problem. With that said, the workaround I have used in the past in Box2D is to attach a dynamic body via a Joint to a kinematic body in order to register collision.

    For example if you want a Kinematic ship to trigger collision with a static wall, then you should attach a thin Dynamic body (via a Joint) to the edge of the wall. The static body of the wall ensures that the wall won't move. The dynamic body of the wall will register collision when kinematic bodies hit it.

    Box2D collision events always need at least one dynamic body present.
     
  27. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    Yeah, it's been explained and I understand it's a Box2D thing.

    Hm, that's interesting. I'd considered something like that, but was hoping to avoid it for efficiency's sake. Kind of a pain to have to do that every time. Guess that's what prefabs are for.

    Do you have any idea WHY Box2D chose not to allow listening for kinematic/static collisions? Seems like it'd be a pretty useful thing in a lot of cases. I suppose it could just be a sacrifice made for the sake of better collision detection all around. Ah well.
     
  28. keely

    keely

    Joined:
    Sep 9, 2010
    Posts:
    967
    This has been fixed in 4.5
     
    OceanBlue likes this.