Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to detect if the player is on ground

Discussion in 'Editor & General Support' started by SunnyMonster, Sep 2, 2021.

  1. SunnyMonster

    SunnyMonster

    Joined:
    Oct 25, 2020
    Posts:
    7
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    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://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

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

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
  3. ElegantUniverse

    ElegantUniverse

    Joined:
    Sep 13, 2018
    Posts:
    78

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


    Code (CSharp):
    1. public CharacterController _characterController;
    2. bool _groundedPlayer ;
    3. void Update()
    4. {
    5. _groundedPlayer = _characterController.isGrounded;
    6. if(_groundedPlayer)
    7. {
    8.  
    9. }
    10. }
    11.  



    or you can use this code for checking your player is on the ground or not :
    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4. if(CheckGround())
    5. {
    6. //the player is on the ground
    7. }
    8. }
    9.     public bool CheckGround()
    10.     {
    11.         _distanceToTheGround = GetComponent<Collider>().bounds.extents.y;
    12.         return Physics.Raycast(transform.position, Vector3.down, _distanceToTheGround + 0.1f);
    13.     }
    14.  
    15.  
     
    spotlessapple likes this.
  4. SunnyMonster

    SunnyMonster

    Joined:
    Oct 25, 2020
    Posts:
    7
    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
    Code (csharp):
    1. Input.GetButtonDown()
    instead of
    Code (csharp):
    1. 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.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    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://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

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

    ElegantUniverse

    Joined:
    Sep 13, 2018
    Posts:
    78
    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.
     
    Last edited: Jul 30, 2022