How to detect if the player is on ground

I’m doing this for a first-person controller. I’ve tried a few things to achieve this. A tutorial by Brackeys uses Physics.CheckSphere which doesn’t detect the ground if the player is on the edge of the ground. I also tried using a trigger collider and placed it under the player but that isn’t very reliable for some reason.
Is there a way, somehow, I can detect if the ground is touching the bottom of the player? The player uses a box collider.

P.S. I’m using a boolean variable to track if the player is on the ground. It is very important that the variable only gets set only once when it touches the ground and only once when it leaves the ground. So I can only use functions like OnCollisionEnter.

Thanks in advance! Any help will be appreciated.

Figure out why. What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

Also, beware of timing of Physics updates versus regular Update() updates.

Here is some timing diagram help:

hello if you are using characterController component for your FPS game , you can use the CharacterController checkground bool same as this code:

public CharacterController _characterController;
bool _groundedPlayer ;
void Update()
{
_groundedPlayer = _characterController.isGrounded;
if(_groundedPlayer)
{

}
}

or you can use this code for checking your player is on the ground or not :

void Update()
{
if(CheckGround())
{
//the player is on the ground
}
}
    public bool CheckGround()
    {
        _distanceToTheGround = GetComponent<Collider>().bounds.extents.y;
        return Physics.Raycast(transform.position, Vector3.down, _distanceToTheGround + 0.1f);
    }

Thank you all for replying.

I tried using the character controller component and it worked flawlessly. Only thing is it uses a capsule collider and what I want is a box collider.

I don’t think this will work if the player is on the very edge of the ground. And a problem with these distance checks is that if the player takes 10 frames to move out of the “ground radius” and the player held down the spacebar for 10 frames too, the force or the velocity will be applied 10 times. I of course can just use Input.GetButtonDown() instead of Input.GetButton()but that wouldn’t allow me to jump continually when holding down space.

This is quite hard to debug. But I think what happened was the trigger collided with something and then exited the collision in the same fixedupdate. And then the OnTriggerEnter and OnTriggerExit functions didn’t get called in the correct order.

If I were to do this without a game engine is to insert some code in my collision detection to check if the collision happened on the bottom face of the player. But obviously I can’t do the same to Unity.

Is there a way (normal or hacky) that I can detect where the collision happened? That seems like the only solution that might work.

Thank you both again for replying.

Of course! Physics.Raycast is one of the worlds most overloaded methods. Several of the overloads accept a RaycastHit, which they will happily fill in with data about what happened.

In the time since we first started this thread, there is another thread you might have interest in:

https://discussions.unity.com/t/855344

It’s Alucard’s attempt to make a one-stop-shop quick-and-dirty proto controller. I think it’s purty cool!

have you ever tried to put an empty game object with a triggered box collider within the player?
you can check the player collider with the ground by using OnStayTrigger method, then you will find out the player is on the ground or not.

im trying this, but how can then implement it on the ontrigger method? i tried adding a collider variable and dragging the empty “hitbox” gameobject, but what then?? if you can help me ill appreciate it, i may be wrong but it seems the ontrigger method only works for the actual gameobject where the script is, or is just simply me that cant know the proper code to detect if the child hitbox object detect a trigger collision :confused: