- Home /
Question by
Insprata · Dec 03, 2018 at 07:54 AM ·
physics2dcollision detectionphysics2d.raycast
Small overlap in ColliderDistance2D after moving collider to RaycastHit2D.centroid
Hello, I'm trying to get a custom 2D collision solver working but am running into this problem where there seems to be a small overlap after I move a capsule collider to its RaycastHit2D.centroid.
In this simplified example, I try to cast at a collider from another the player and move the player to the centroid. Then, when I call Physics2D.Distance on the player and the collider, I assume that the two should not be overlapping but touching, however, there is always a small overlap between the two colliders, whose distance seems to change depending on what part of the capsule was closest to the hit collider and the normal of the hit plane.
What causes this and is there a way to get rid of this overlap?
void RaycastAndCheckDistance(){
RaycastHit2D hit = Physics2D.CapsuleCast(/* parameters here */);
if(hit){
transform.position = hit.centroid;
ColliderDistance2D colliderDistance = Physics2D.Distance(this.collider, hit.collider);
Debug.Log("isValid: " + colliderDistance.isValid); //returns true
Debug.Log("isOverlapped: " + colliderDistance.isOverlapped); //returns true
Debug.Log("Distance: " + colliderDistance.distance); //returns small negative distance - usually between near-0 to -0.03~ depending on the scenario
}
}
Comment