I have been using a CapsuleCollider2d in conjunction with a Tilemap (with Colliders) and it gets stuck in small gaps, even though there is room (see example screenshot).
Here is my code for movement (_rigid is the attached RigidBody2D and moveDirection is the intended direction of movement. Once it happens the collider is completely stuck. I have low friction (0.3) on the physics materials of both colliders. TerminalVelocity is to clamp the maximum velocity past a certain point. I have got the collision detection set to Continuous.
private void FixedUpdate()
{
if (_rigid.isKinematic)
{
transform.Translate(moveDirection);
}
else
{
{
_rigid.AddForce(moveDirection);
}
Vector2 vel = _rigid.velocity;
Debug.Log(vel);
bool changed = false;
// Clamp
if (vel.x < -TerminalVelocity)
{
vel.x = -TerminalVelocity;
changed = true;
}
else if (vel.x > TerminalVelocity)
{
vel.x = TerminalVelocity;
changed = true;
}
if (vel.y < -TerminalVelocity)
{
vel.y = -TerminalVelocity;
changed = true;
}
else if (vel.y > TerminalVelocity)
{
vel.y = TerminalVelocity;
changed = true;
}
if (changed)
{
_rigid.velocity = vel;
}
}
}