Raycast always colliding (SOLVED)

I’m trying to draw a simple RayCast to see if I’m on the ground or not:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeTest : MonoBehaviour {

    public float mouseSensitivity = 15.0f;
    public float moveSpeed = 15.0f;
    
    //this handles the camera rotation
    private float camRot;
    private float distToGround;

	// Use this for initialization
	void Start () {

        //initialize the camRot to the starting rotation
        camRot = 30.0f;
        //initialize the camera child rotation
        transform.Find("Main Camera").localRotation = Quaternion.Euler(30.0f, 0, 0);

        //Gets the distance to the bottom of the capsule collider
        distToGround = GetComponent<Collider>().bounds.extents.y;

    }
	
	// Update is called once per frame
	void Update () {

        MoveMe();

    }

    void MoveMe()
    {
        //get the X axis rotation used for the game object rotation
        var xRot = Input.GetAxis("Mouse X") * Time.deltaTime * 20.0f * mouseSensitivity;

        //get the rotation used for the camera child rotation
        camRot += Input.GetAxis("Mouse Y") * Time.deltaTime * 10.0f * mouseSensitivity;

        //get the X and Z axis movement used for the game object movement
        var xTran = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        var zTran = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

        //clamp the rotation for the camera
        camRot = Mathf.Clamp(camRot, -30f, -7.5f);

        //set the rotation for the camera
        transform.Find("Main Camera").localRotation = Quaternion.Euler(-camRot, 0, 0);

        //rotate the game object
        transform.Rotate(0, xRot, 0);

        //move the game object
        transform.Translate(xTran, 0, zTran);

        
        if (Input.GetKeyDown("space") && IsGrounded())
        {
            gameObject.GetComponent<Rigidbody>().AddForce(transform.up * 10f, ForceMode.Impulse);
        }

    }

    bool IsGrounded()
    {
        return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.3f);
    }

}

No matter what I do, I can still jump in midair infinitely. Why is the check not working?

  1. What does your “ground” object look like?

  2. Use Debug.DrawRay to see if your raycast works correctly.

  3. “Why is this not working?” is not really a good question. Be more specific when creating new posts.

@LCStark
My ground is a terrain. I added a Debug.DrawRay and saw that the size of my Raycast was indeed off, but that was not helpful to solving my problem. It only prevented a further problem.

I’m not sure what better question I could really ask. The code is extremely simple. It should draw a line straight down to just a little farther than the bottom of my object’s collider, which it does, and check to see if there is anything there, which it does. It should return true if there is, and false if there isn’t, which it sometimes does, but sometimes it will randomly return true for no reason. What question can I ask that’s more specific? “Why is it returning true when there’s nothing there”?