How to do a functional Ground Test?

Hi guys, I’m trying to do an “On Ground Test” in my rigid bodies but there is away a problem, the grounded bool is update too fast and I can’t achieve the desired effect.

RaycastHit hit;
if(Physics.Raycast(transform.position, -transform.up , out hit, rayDistance))
{
		if(hit.collider.tag == "Platform")
		{
                   grounded = true;
            }
			
}  

if(grounded)
{
    animation.Play(Run);
}

if(JumpButtonPressed)
{
    grounded = false;
    animation.Play("Jump");
    DoJump();
}

The Raycast, even if it’s very small, will touch the platform before the player can get height and grounded will be set to true, therefore playing the “Run” animation while in mid-air, how to deal with that?

Hmmm… I personally use a different method of detecting whether my player is grounded, but I have been encountering some very strange problems regarding jumping against objects increasing my jump height that have yet to be isolated, so test excessively if you try my suggestion. Rather than raycast or rely solely on function calls my code checks the distance a collision occurred on my playerCollider relative to the bottom of the collider… If the distance from feet to collision is less than the radius of my capsule collider then I know my player is standing on something. This method is of course expesive because I have to check every collision point every OnCollisionEnter event but it prevents wall jumping and will always trigger exactly when the collider hits the ground no sooner no later and is accurate on rough terrain. An exert of my code…

//Potentially very computationally expensive
private void OnCollisionStay (Collision collisionInfo) {
		
	foreach (ContactPoint contact in collisionInfo.contacts) {
			
		//Only allow collisions on the lower hemispere of the collider to ground the character
		if ((contact.point - transform.position).magnitude <= BodyCollider.radius) {
				
			Grounded = true;
			break;
				
		}
			
	}
		
}

It is worth noting that my CapsuleCollider is aligned to my player so that the very bottom of the collider is the position of the player rather than the position being in the center of the collider.