Odd Raycast behavior

Hi guys,

I’m trying to check if a ledge is on the ground but the raycast is acting in a weird way. The code keep cycling between “No Ground” and “Can see ground” but never enters “On Ground”. Physic is acting well, the ledge collide with object…

void CheckOnGround()
	{
		RaycastHit hit;
		float distance;

		if(Physics.Raycast(new Vector3(transform.position.x, transform.position.y+20,transform.position.z),Vector3.down, out hit,50f))
		{
			
			Debug.DrawLine(transform.position, hit.point);
			distance = Vector3.Distance(transform.position, hit.point);
			
			if(distance < 0.03f)
			{
				IsOnGround = true;
			}
			else
			{
				IsOnGround = false;


			}
		}
		
		else
			
		{
			IsOnGround = false;

		}
	}

You are using float, a float is barely 0.0000000000 but more likely having a little 1 here and then.

You would be really lucky if your value ever gets to an exact 0.000000000 (dunno exactly how many 0 should be there). You 'd rather use a double comparison like:

if(value<0.005&&value>-0.005)//something