I noticed this issue where the box collider 2D of my player does not touch the box collider 2D of the object it is landing on. I originally noticed this on a tilemap, but even after simplifying the problem down to just a couple very simple objects, I’m experiencing the same exact issue.
The top box just has a sprite renderer, box collider 2D, and a rigidbody 2D.
The bottom box just has a sprite renderer and a box collider 2D.
All of the default settings were used, and the collider matches the shape of the boxes exactly. Any ideas why I might be experiencing this issue?
Managed to find help outside of these forums, and I thought I’d leave the answer for anyone else facing the same issue.
Under Project Settings → Physics 2D, there is a setting called “Default Contact Offset”. This was set to 0.01 by default. Decreasing this value fixed the issue.
That’s not a good solution at all. The warning on that option is there for a reason. All you’ve done is reduce the buffer region that Box2D requires for accurate collision detection. This small gap is there for stability. If you’re fine with this and know what you’re doing then it’s all good but I wanted to reiterate this.
The fact that a 10mm gap is significant means your rendering scale is quite small. This gap is supposed to be visually insignificant but numerically significant.
Either increase your scale or reduce the collider size with respect to the visuals.
But that’s not related to this thread. You can back-off the result from a Line/Raycast along the direction by the Physics2D.defaultContactOffset value but only when it’s a polygon. Circles don’t use this.
Note sure why you use the property then some arbitrary constant as that’s wrong.
You’re right, i did not need this extra value, i thought the physics offset would put me right before the limit between two objects.
I was trying to do a raycast check between my character and the ground, but it didn’t register. Zooming in showed my that the colliders were indeed not touching. This led me here.