Search Unity

OnCollisionEnter2D not being called

Discussion in '2D' started by gremklinemkline, Apr 12, 2022.

  1. gremklinemkline

    gremklinemkline

    Joined:
    Feb 12, 2021
    Posts:
    5
    Hello, I'm really confused as to why my OnCollisionEnter2D method is not being called. Both Objects have Box Collider 2Ds attached, and the colliders collide, yet nothing happens, does anyone know what is causing this?

    upload_2022-4-12_17-53-34.png upload_2022-4-12_17-53-47.png
    upload_2022-4-12_17-54-31.png
    Very simple stuff, but I'm baffled as to why the method isn't called, any help would be highly appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Very simple stuff indeed. Did you read the general docs on collisions and are you meeting the requirements of same, specifically the required presence of a Rigidbody2D and what it implies about where your script needs to be?
     
  3. gremklinemkline

    gremklinemkline

    Joined:
    Feb 12, 2021
    Posts:
    5
    Yeah I've read through them, I didn't think that I needed a rigidbody2D though, as I'm not using any physics, only the colliders, am I wrong?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Yeah, you need it... the Rigidbody(2D) is what actually generates all appropriate trigger / collision messages.
     
  5. gremklinemkline

    gremklinemkline

    Joined:
    Feb 12, 2021
    Posts:
    5
    ah ok, thanks very much!
     
    guedeseder9 and Kurt-Dekker like this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,496
    If you don't add a Rigidbody2D then the Colliders are implicitly Static (non-moving, non-changing). Static do not contact Static because they would have no need to because they don't move and don't change.

    If you add a Rigidbody2D to both and set both their body-type to Static, it'd be the same.

    Dynamic body-type does contact every other body-type. This is why it's often stated "you need to add a single Rigidbody2D" but in-fact, it's implicity saying you need one to be Dynamic.

    Static vs Static = No
    Static vs Kinematic = Only if UseFullKinematicContacts is on
    Kinematic vs Kinematic = Only if UseFullKinematicContacts is on
    Dynamic vs anything = Yes, always.

    This suggests above that you're modifying the Transform to "move" the collider. This isn't moving the collider, it's causing the Static collider to be completely recreated during the next simulation step at the new position.

    If it moves in 2D physics, it's the Rigidbody2D that should move and you should get it to move by exclusively using the Rigidbody2D API and never modify the Transform. A Rigidbody2D is a physics component and should not be controlled how you would (say) a renderer.
     
  7. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    Does the requirement to use rigidbody instead of transform API also apply to kinematic rigid bodies or only dynamic ones?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,496
    That's not related to body-type. You should never drive via the Transform. The main thing to understand here is that the Rigidbody2D role is to write to the Transform after the physics simulation, not the other way around. It acts as a proxy to the Transform because you want physics behaviour.

    Does "it work" if you write to the Transform and force the Rigidbody2D to read from the Transform? Yes! It works like that because otherwise it'd be reported as a bug and so we're forced to "support" it. Unfortunately, this hides the fact that you shouldn't be doing it because it's not physical movement through space, it's just instant teleportation to the new position and isn't as performant.

    Doing it on Kinematic bodies isn't so bad as doing it on Dynamic bodies because for them, they also have a collision response and instantly teleporting into a new position means you can cause overlaps which the solver has to solve.

    In short, if it moves it should be the Rigidbody2D moving and you should get it to move by using its API. Always, no exceptions. If you do this, you won't hit any unexpected things by modifying the Transform. Things such as interpolation for smooth movement not working.
     
    firebird721 and Kurt-Dekker like this.
  9. gremklinemkline

    gremklinemkline

    Joined:
    Feb 12, 2021
    Posts:
    5
    I've refactored my code a bit in line with this, but it still doesn't seem to be working, screenshots below. I've attached RigidBody2D components to the things that need them, but my OnCollisionEnter2D method still isn't being called, any ideas?
    upload_2022-4-13_16-23-40.png
    The Components for the moving box
    upload_2022-4-13_16-24-58.png
    The Box moving into a wall
    upload_2022-4-13_16-23-52.png
    The code for the moving box
    upload_2022-4-13_16-24-37.png
    The components on the box
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    As this is a technical discussion, be clearer on this point: is it at least MOVING? Or is nothing happening?

    Only thing you're not showing is to make sure neither collider is marked Trigger, and also that it isn't vanishingly small in its internal dimensions.

    If you can't get it working, make a blank scene, two quads one above the other spaced apart vertically, RB2D on the top, box collider on bottom, your CollisionTest on the top and press Play.

    This does work.
     
  11. gremklinemkline

    gremklinemkline

    Joined:
    Feb 12, 2021
    Posts:
    5
    The Box moves fine, neither is marked as isTrigger, and the dimensions are (1,1,1) and (1,10,1) respectively.
    This is the blank scene where I've remade it unfortunately, so I'm really not sure where I'm going wrong
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I probably have 20 of these things by now, but they're not even worth finding... it takes like 3 minutes to make a new one. See enclosed.

    EDIT: included a script-moved one

    Screen Shot 2022-04-13 at 9.12.46 AM.png Screen Shot 2022-04-13 at 9.12.44 AM.png
     

    Attached Files:

    firebird721 likes this.