# Collision resolution using colliderdistance

Hey there
I am trying to write a character controller with no rigidbody, but using physics2d overlap functions. The basic idea is to translate the transform, check for overlaps, use the colliderdistances obtained to displace the transform again.

However I get my object constantly going through walls. I am not sure if this is a floating number precision problem or some misunderstanding of the functions I’m using. If the former, then I have to set flags for contact and manually place the transform at the contact point, if it’s the latter then I need to understand better what is happening.

Here’s a minimal code example:

``````        //this is all inside Update(). Also, this requires boxcollider2d in the same gameobject
//first alter velocity in whatever way you want...

transform.Translate(velocity * Time.deltaTime); //velocity is a vector2 or 3

//collision and resolution
Collider2D[] hits = Physics2D.OverlapBoxAll(transform.position, boxCollider.size , 0); //boxCollider is the BoxCollider2D of the gameobject
foreach (Collider2D hit in hits)
{
if (hit == boxCollider)
continue;

ColliderDistance2D coldist = hit.Distance(boxCollider);

if (colliderDistance.isOverlapped)
{
transform.Translate(coldist.pointA - coldist.pointB);

}
}
``````

At first I thought I could be introducing errors by calling transform.Translate several times. So I changed the code to instead change a displacement variable and at the very end I apply displacement with one Translate. It did exactly the same thing. Translate is probably doing that under the hood, adding up all its arguments and applying at the end of the frame.

Found one problem. I stupidly kept increasing velocity in my code (to make it fall) and so that always increasing velocity.y was messing things up.

Now that I cancel velocity normal to the collision things behave much better. Still, I see some penetration and bouncing but that does look like float number precision error.

Because movement increments are computed discretely (and not continuously), it is possible for an object to appear on the wrong side of an obstacle’s collider after a translation operation. When hit.Distance() is called, it produces a vector that actually pushes the character further in the wrong direction. This can amount to an effect where the character teleports across a wall.