CapsuleCollider getting stuck

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;
            }
        }
    }

Most likely it’s getting a ghost collision from the multiple shapes below it there. Multiple shapes do not form a continuous surface so ghost collisions occur. Try temporarily using the composite collider in outline mode to check that as this produces continuous edges. Beyond that it’s hard to tell.

NOTE: Do not modify the Transform when using physics components, that’s the job of the Rigidbody2D itself. Use the Rigidbody2D API to do that indirectly.

Thanks - I’ve just tried with a compositecollider2d (a much better solution for my case anyway) and its doing the same thing. The Transform.Translate in the code above was just for when the object is set as Kinematic (i.e. outside of player control) and I need to manually move it - in my case above I’m using AddForce.

On another note as you can see the auto-generated collider tends to have a gap around it - is there any way to make it exactly follow the sprite edges?

6122465--667220--composite.jpg

Yes, you need to edit the physics shape for the tile sprite yourself in the sprite editor window. Those auto-generated ones are very basic so you should always refine them yourself.

I have noticed that the auto-generation is always 2 pixels out from the edge (despite setting the outline tolerance to 0).

I would like it to do this:

Having to do a couple of hundred of these so very tedious :frowning: I’m not sure what the outline tolerance actually does - it doesn’t seem to make any difference to the generation. Also a select all would be very handy here for Generate.