Need a better understandable "IsGrounded" script for Rigidbody.

This is what i use right now:

using System.Collections;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float gravity;

    public bool IsGrounded;

    void Start()
    {

    }


    void Update()
    {
        if (IsGrounded)
        {
            if (Input.GetButtonUp("Jump"))
                this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * gravity);
        }

    }

    void OnCollisionStay(Collision collisionInfo)
    {
        IsGrounded = true;
    }

    void OnCollisionExit(Collision collisionInfo)
    {
        IsGrounded = false;
    }
}

As soon as the “Player” hit a wall it says that it is grounded so i can spam the Jump button to get up the wall. Anyone that knows a fix for this or has a better Jump script?

Hey there, Corbin M here.
Something I would do for this would be the use of unity’ s

Physics.SphereCast:
(Unity - Scripting API: Physics.SphereCast)
What I would do in this case:
I would not use the bool “isGrounded”
Instead, what I would do is replace that condition in the if statement right under your update with…

RaycastHit hit;
If (Phisics.SphereCast(transform.position, player_radius, Vector3.down, hit, (player_height / 2) + 0.1f))
Rest of code…

Player_radius will be whatever you want it to be. Recommended that it is the thickness of your character though.
Player_height will be the height of your player.
Make public variables of these to modify them to see where the sweet spot it that you’ll like.

With this, you can detect an area under your player and return true if your player is near the ground.

I hope this helped.

Sorry for the slop text. I’m on my phone and I can’t do anything fancy.

You can simply do something like this…

     void OnCollisionStay(Collision collisionInfo)
     {
         if (other.gameObject.name == "The Name Of Your Ground Game Object" ) { // using Tags instead of name checking is recommended.
              IsGrounded = true;
              }
     }
 
     void OnCollisionExit(Collision collisionInfo)
     {
         if (other.gameObject.name == "The Name Of Your Ground Game Object" ) {
         IsGrounded = false;
         }
     }

Acually to handle is grounded it is better to use several conditions. You can use your y velocity for this. Hovewer if your rigidbody is sliding down (With a force of a physics) game will think that your character in the air.

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

public class Example : MonoBehaviour {
    Rigidbody rigid;
    bool IsGrounded;

	void Start () {
        rigid = GetComponent<Rigidbody>();
	}
	

	void Update () {
        if (rigid.velocity.y == 0)
            IsGrounded = true;
        else
            IsGrounded = false;
	}
}

Also you can use a raycast that is pointing down with a height of your character and if it is meeting a ground your character is on the ground. It is also better to check both of this conditions simultaniously.

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

public class Example : MonoBehaviour {
    public float Height;
    bool IsGrounded;
    Ray ray;

	void Update () {
        if (Physics.Raycast(transform.position,Vector3.down, Height))
        {
            IsGrounded = true;
        }
        else
        {
            IsGrounded = false;
        }
	}
}