In my game, I have an GameObject that I want to affect the player with OnCollisionEnter2D. The problem is, I don’t want the player to treat this GameObject like ground. In other words, both GameObjects have gravity, treat a Composite Collider 2D as ground, and I don’t want these two GameObjects to treat each other as ground (but still register overlaps). Is there any function or variable I can use to help me?
Tags are nothing to do with the physics engine. This is what layers are for. This is why all the physics queries provide the ability to query via layer.
Tags are for you. They are slow and use string compares. They are not for the physics system which uses the fast layer-masks.
This is why we have a Layer Collision Matrix which is a basic thing to learn when using Unity physics.
Sorry, I meant to say layers, but I said tags. I tried using the Layer Collision Matrix, but OnCollisionEnter2D didn’t register.
Then you’ve done something wrong. Assign the layer on the GameObject that the colliders are on, ensure they’re checked in the Layer Collision Matrix and they’ll collide. TBH, you’re saying that callback never happened but did they collide?
So many things you can be doing wrong like expect Static colliders to contact Static colliders for instance. Not adding a Rigidbody2D because it’s not considered something required etc.
Really though, if you follow any of the basic tutorials on physics then you should easily get this working. Start a fresh scene, add a couple of objects and watch them collide or not collide. Likely that you’ve got a lot of things in flux and have got confused over something which in itself isn’t an issue, just step back and understand how it works in an empty scene/project is best.
If none of the above applies to you then you need to post some configuration/script info because you cannot expect an answer without anyone seeing what you’re doing.
Never mind. What I did was remove the collision between the two layers in Layer Collision Matrix, and used Physics2D.Boxcast to register “collisions”. Thanks, anyway!