UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

physics

[2D Physics] Stop BoxCollider2D Sticking to Vertical Walls

Solution

physics2dcollisioncollidertriggers

Unity 2021.x - Unity 6.3.x

Published 7 days ago

Issue

 A BoxCollider2D is observed to exhibit sticking behavior when interacting with walls, particularly when combined with a SphereCollider2D on the same GameObject. This interaction leads to unintended halts or immobility against surfaces during vertical or horizontal traversal.

To prevent 2D colliders from sticking to walls, apply a Physics Material 2D with zero Friction to the BoxCollider2D and set Rigidbody2D detection to Continuous.

Explanation

To address instances of 2D collision sticking, a Physics Material 2D should be assigned to the BoxCollider2D component. The properties of this material can be configured to minimize Friction, which is often a primary cause for colliders becoming momentarily adhered to environmental geometry.

  1. Create a Physics Material 2D in your Project window.
  2. Set the Friction value to 0.
  3. Assign this material to the Material slot on your BoxCollider2D or the Rigidbody2D component.
  4. Adjust the dimensions of the BoxCollider2D to be slightly larger than the SphereCollider2D. This configuration ensures the spherical collider does not make direct contact with wall surfaces, allowing the Friction-less box to manage the primary collision.
  5. Set the Collision Detection property on the Rigidbody2D component to Continuous to increase precision and prevent objects from sinking into geometry.

Additional Tips

  • Use a CompositeCollider2D on your environment tilemaps to eliminate internal seams that catch on colliders even with zero Friction.
  • Consider replacing the dual-collider setup with a single CapsuleCollider2D to provide smoother interpolation over uneven surfaces.

Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.