I am trying to write my own 3D collision resolving logic. I want to write this myself for learning purposes.
void OnCollisionStay(Collision col)
{
Vector3 HitVector = transform.position - col.contacts[0].thisCollider.ClosestPointOnBounds(transform.position);
transform.Translate(col.contacts[0].normal * CurrentSpeed, Space.World);
}
It kind of works. The object hitting the wall is pushed back by the normal of the thing it hit, but the distance it is moving is wrong, causing jerky movement:
The multicolored line is the history of positions of the car driving North. You can see it zig zags a bit as it is pushed off the wall further than its collision bounds, causing it drive forward for a few frames before hitting the wall again.
What is the correct way to calculate the offset require the put the collider against the edge of the wall?