Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

CharacterController.IsGrounded Not Working

Discussion in 'Scripting' started by GrayWoolsey, Jul 12, 2020.

  1. GrayWoolsey

    GrayWoolsey

    Joined:
    Apr 20, 2018
    Posts:
    27
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CharacterLocomotion : MonoBehaviour
    4. {
    5.     private CharacterController controller;
    6.  
    7.     private Animator animator;
    8.     private Vector2 input;
    9.  
    10.     private float verticalVelocity;
    11.     [SerializeField] private float stickToGroundForce = 10.0F;
    12.     [SerializeField] private float jumpForce = 8.0F;
    13.     [SerializeField] private bool isGrounded;
    14.  
    15.     void Start()
    16.     {
    17.         controller = GetComponent<CharacterController>();
    18.         animator = GetComponent<Animator>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         //Get Horizontal Inputs
    24.         input.x = Input.GetAxis("Horizontal");
    25.         input.y = Input.GetAxis("Vertical");
    26.         //Animate The Character Based On Input
    27.         animator.SetFloat("InputX", input.x);
    28.         animator.SetFloat("InputY", input.y);
    29.  
    30.         isGrounded = controller.isGrounded;
    31.  
    32.         if (controller.isGrounded)
    33.         {
    34.             verticalVelocity = -stickToGroundForce * Time.deltaTime;
    35.             if (Input.GetKeyDown(KeyCode.Space))
    36.             {
    37.                 verticalVelocity = jumpForce;
    38.             }
    39.         }
    40.         else
    41.         {
    42.             verticalVelocity -= stickToGroundForce * Time.deltaTime;
    43.         }
    44.  
    45.         Vector3 moveVector = new Vector3(0, verticalVelocity, 0);
    46.         controller.Move(moveVector * Time.deltaTime);
    47.     }
    48. }
    controller.isGrounded is only calling true while I am moving and not standing still. I couldn't find any other answers that seem to have worked online. Not only does it not work while I am standing still it also doesn't work while walking down stairs or slopes where as walking up them works fine. Any help would be appreciated.
     
  2. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    your code for determining if the player is grounded is in the controller script, which you're referencing in this script, but aren't actually showing, so there's no way to determine whats wrong based off of what's here.
     
  3. GrayWoolsey

    GrayWoolsey

    Joined:
    Apr 20, 2018
    Posts:
    27
    The "controller" is a reference to the CharacterController component which is retrieved in the Start Function so it's Unity Engine's method of checking ground collision.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    How are you actually moving your character? I only see code for jumping and animating.
     
  5. GrayWoolsey

    GrayWoolsey

    Joined:
    Apr 20, 2018
    Posts:
    27
    The animation is what actually moves the character, I learned it from a very nice tutorial on YouTube and it's actually very basic and reliable. Here it is in action, I warn you mute the video. My mic is buzzing like hell and I don't know why lol.



     
  6. GrayWoolsey

    GrayWoolsey

    Joined:
    Apr 20, 2018
    Posts:
    27
    I've also fixed what I was trying to fix with help from taking some information from a Brackeys video. It now works as expected and I replaced the entire ground checking and jumping in my script with this.

    Code (CSharp):
    1. #region Jumping And Ground Check
    2.  
    3.         //Phsyics Check To Ground
    4.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    5.  
    6.         //If The Player Is On The Ground Stick To Ground And Reset Vertical Velocity
    7.         if (isGrounded && verticalVelocity < 0)
    8.         {
    9.             verticalVelocity = -stickToGroundForce;
    10.         }
    11.  
    12.         //Jump If The Player Is Grounded
    13.         if (isGrounded && Input.GetButtonDown("Jump"))
    14.         {
    15.             verticalVelocity += jumpForce * stickToGroundForce;
    16.         }
    17.  
    18.         //Gravity
    19.         verticalVelocity -= characterGravity * Time.deltaTime;
    20.  
    21.         //Apply These Calculations To The Actual Player Controller
    22.         Vector3 fallVector = new Vector3(0, verticalVelocity, 0);
    23.         controller.Move(fallVector * Time.deltaTime);
    24.  
    25.         #endregion
     
    479813005 likes this.
  7. lollampat

    lollampat

    Joined:
    Dec 6, 2020
    Posts:
    1
    I fixed this issue Setting Min Move Distance Character Controller value to 0. Now, controller.isGrounded works just as expected. I found it in a forum and it seems to work fine.
     
  8. MagyarPeter

    MagyarPeter

    Joined:
    Nov 28, 2020
    Posts:
    11
    You have to push the charactercontroller to the ground with the gravity, and check the isgrounded after the "move" function.

    public float gravity = 10f;
    public float maxFallSpeed = 10f;
    CharacterController cc;
    bool ground = false;

    void Start()
    {
    cc = GetComponent<CharacterController>();​
    }

    void Update()
    {
    if(ground)
    {
    Y = Mathf.Clamp(Y, -0.1f, Mathf.Infinity);​
    }
    else
    {
    Y = Mathf.Clamp(Y - gravity * Time.deltaTime, -maxFallSpeed, Mathf.Infinity); //This is just a basic gravity,
    the point is to take the character down when it’s in the air, but don’t stop it from jumping.​

    cc.Move(new Vector3(0, Y, 0) * Time.deltaTime); // insert here the motion values
    ground = cc.isGrounded; //check the isgrounded after the "move" function, like this.​
    }​
     
  9. Anisoropos

    Anisoropos

    Joined:
    Jul 30, 2012
    Posts:
    102
    For anyone doing custom gravity
    Code (CSharp):
    1.  
    2. // Always keep "pushing it" to maintain contact
    3. if (collider.isGrounded)
    4.     velocity_Gravity = gravity.normalized * 0.01f;
    5. // Accelerate
    6. else
    7.     velocity_Gravity += gravity * Time.deltaTime * gravityMulti;
    8.  
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    While we're necroing old threads, do not use the Unity sample code for CharacterController.

    I wrote about this before: the Unity example code in the API no longer jumps reliably.

    I reported it in October 2020. Here is a work-around:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746

    I recommend you also go to that same documentation page and ALSO report that the code is broken.

    When you report it, you are welcome to link the above workaround. One day the docs might get fixed.
     
    Fenikkel likes this.
  11. Uios-Theou

    Uios-Theou

    Joined:
    May 10, 2018
    Posts:
    8
    Code works fine if you switch the minimum distance detection in the character controller component from 0.001 to 0.
     
  12. DavidZobrist

    DavidZobrist

    Joined:
    Sep 3, 2017
    Posts:
    233
    @MagyarPeter solution worked

    Add constant -y movement to the controller and the detection works fine.


    Code (CSharp):
    1.  
    2.  
    3. [SerializeField] float gravity = -4f;
    4.  
    5. moveDirection = new Vector3(
    6.                             Input.GetAxis("Horizontal")* walkSpeed,
    7.                             gravity,
    8.                             Input.GetAxis("Vertical")* walkSpeed);
    9.                                  
    10. charController.Move(moveDirection * Time.deltaTime);
    11.  
    12. Debug.Log(charController.isGrounded);
    13.  
     
  13. thisisajaikumar

    thisisajaikumar

    Joined:
    Apr 22, 2020
    Posts:
    5
    Adjust the skin width in character controller may be sometimes it will working for you. in my case i adjusted skin width 0.8 to 0.0001 it perfectly worked for me Screenshot 2021-10-15 191338.png work.png
     
  14. HunterAhlquist

    HunterAhlquist

    Joined:
    Oct 6, 2013
    Posts:
    129
    This didn't work for me, and I am using a gravity function to push the player down.
     
  15. mpbMKE

    mpbMKE

    Joined:
    Aug 13, 2019
    Posts:
    3
    Same. I can zoom in on the collider after it drops and it's actually sticking into the ground and still reports as "False." If I just remove the conditional to check if it's true, it will report true. Which I guess would be fine if I didn't have conditions I needed to check, lol.
     
  16. alikingames

    alikingames

    Joined:
    Sep 10, 2021
    Posts:
    1
    Guys just set Min Move Dis tance 0.001 to 0
     
  17. xxhaissamxx

    xxhaissamxx

    Joined:
    Jan 12, 2015
    Posts:
    134
    work fine thanks
     
  18. xeosD

    xeosD

    Joined:
    Apr 6, 2023
    Posts:
    9
    to be clear while setting the min move distance to 0 helps a lot, it still will fail to detect the ground when it's probably just a couple centimeters below your feet. Is there a way to increase the distance threshold for counting as grounded without doing an additional collision detection?