The grounded bool of my jump function doesn't become true, using ray cast

Hi all,

Would be grateful for some help.

I’ve created a capsule with a box collider. I’ve attached an empty game object to the bottom of the capsule. The game is on a 2D plane.

I’ve created a jump function using rigid body.addforce. I’ve also attached a ray cast script onto the empty gameObject at the base of my capsule (the player).

I believe I’ve set it so that, every time the ray cast detects anything below it (in my case, a plane that I’m using as the floor), it should change a bool value (I’ve called it Grounded) to true. As long as Grounded is true, it should allow me to access a Jump function on another script to allow my player to jump.

Once my player jumps, I should have a script that allows me to change the Grounded bool value back to false, so that my player can’t double jump by accident.

However, after I have jumped once, when my character lands back onto the floor again, the bool value remains on false. Is there something wrong with my script?

The jump function on my playerMove script

void Update () 
	{
		transform.rotation = Quaternion.Euler (lockPos, lockPos, lockPos);

		Vector2 x = Input.GetAxis ("Horizontal") * transform.right * Time.deltaTime * speed;

		transform.Translate (x);

// If "Jump" is pressed and player grounded == true

		if (Input.GetButtonDown ("Jump") && (grounded)) 
		{
			Jump ();
		}

	}

//Player jumps and the grounded bool value becomes false to prevent double jump
    public void Jump()
    	{
    		rigidbody.AddForce(Vector2.up * jumpForce);
    		Debug.Log ("Jump has been pressed");
    		grounded = false;
    		Debug.Log (grounded);
    	}

The ground check script on my empty game object that holds the ray cast on the character

    private float distanceToGround = 0.15f;
    public Transform player; //allows me to access the player move script
 

   	public LayerMask GroundLayer;
void Update () 
    {
    	

	PlayerMove playerScript = player.GetComponent<PlayerMove>();
        
        		RaycastHit hit;
        
        		Ray groundedRay = new Ray (transform.position, -Vector2.up);
        
        		Debug.DrawRay (transform.position, -Vector2.up * distanceToGround);
    
    //If the ray cast hits something below, the grounded bool value becomes true?
        		if (Physics.Raycast (groundedRay, out hit, distanceToGround)) {
        						playerScript.grounded = true;
        						Debug.Log ("Player is touching ground");
        		}
        	}

PlayerMove playerScript = player.GetComponent();

                RaycastHit hit;
 
                Ray groundedRay = new Ray (transform.position, -Vector2.up);
 
                Debug.DrawRay (transform.position, -Vector2.up * distanceToGround);
 
    //If the ray cast hits something below, the grounded bool value becomes true?
                **if (Physics.Raycast (groundedRay, out hit, distanceToGround)) {
                                playerScript.grounded = true;**
                                Debug.Log ("Player is touching ground");
                }
            }

The bolded part is probably causing the problem. Are you sure playerScript.grounded actually writes to grounded? Is grounded a public/private variable?