Using raycast to determine if player is grounded.

So I’ve been trying to use Physics,Raycast without much luck at all. I’ve got a small empty Game object affixed right below the player, which is supposed to send a ray downwards and tell if it’s colliding with anything. When I initially run the code, for a brief moment, it returns “is grounded”, then switches to “is not grounded” and never switches back, whether I jump or not. I’m especially confused because I feel like I’m doing everything right, and when I switch the direction of the ray to up (the direction of the player), it comes back as true. It’s only when sending it downwards to the BoxCollider I’m using as ground does it not see it.

Here’s my code

void Update () 
		{
	
		if (Physics.Raycast(transform.position, transform.TransformDirection (new Vector3(0,-1,0)), .1f, mask))
		{
			Debug.Log ("Is grounded");
		}
		else
		{
			Debug.Log ("Is not grounded");
		}

And as far as the direction, I’ve tried manually inputting the Vector3, I’ve tried using -Vector3.up, and I’ve tried assigning Vector3.up to a variable, and using the negative of that variable. None of it works.

Attach it to your player colider .

var distToGround: float;

function Start () {
     distToGround = collider.bounds.extents.y;
}
 
function IsGrounded(): boolean  
{
  return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
 
function FixedUpdate () {
 
if (IsGrounded())
    {
     }