OnCollisionEnter2D not being called

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?



Very simple stuff, but I’m baffled as to why the method isn’t called, any help would be highly appreciated.

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?

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?

Yeah, you need it… the Rigidbody(2D) is what actually generates all appropriate trigger / collision messages.

ah ok, thanks very much!

2 Likes

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.

3 Likes

Does the requirement to use rigidbody instead of transform API also apply to kinematic rigid bodies or only dynamic ones?

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.

2 Likes

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?
8044871--1038038--upload_2022-4-13_16-23-40.png
The Components for the moving box
8044871--1038047--upload_2022-4-13_16-24-58.png
The Box moving into a wall
8044871--1038041--upload_2022-4-13_16-23-52.png
The code for the moving box
8044871--1038044--upload_2022-4-13_16-24-37.png
The components on the box

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.

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

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

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

8045006–1038083–ReportHit2D.unitypackage (5.29 KB)

1 Like