Collision contacts problem

I’ve written a little bit code to detect when my character hits the edge of a platform:

   if(!hit.collider.gameObject.CompareTag("Platform")) return;
    	if(hit.contacts[0].normal == -Vector3.forward){
    		print("hit side");
    	} else {
    		print(hit.contacts[0].normal +" : "+ -Vector3.forward);

for debugging purposes I wanted to print the normal of the ContactPoint if it doesn’t equal the back of the platform.
but instead of the IF statement detecting that this is the case instead it prints (0.0, 0.0, -1.0) : (0.0, 0.0, -1.0) now im my book this shows that the IF statement is getting it completely wrong… what possably could cause such a thing?

You probably need to use “Mathf.Approximately”. The floating point might be very similar but does not equals due to precision on CPU/GPU levels so you need an approximate.


if ( Mathf.Approximately(hit.contacts[0].normal.x, -Vector3.forward.x)
     && Mathf.Approximately(hit.contacts[0].normal.y, -Vector3.forward.y)
     && Mathf.Approximately(hit.contacts[0].normal.z, -Vector3.forward.z) )
         print("hit side");
} else 
     print(hit.contacts[0].normal +" : "+ -Vector3.forward);