Section 5.36: IsGrounded method not functioning? Unable to Jump.

  • Hey there everyone! I am currently following the video tutorial series and have run into an issue with the optimized player jump code. I followed along and made sure I had code similar to Jon's (except for my choice of using a private layer mask instead of the bit shift), but my jump code is not executing. I am still able to move my character left and right, but it is not responding to the space bar presses. Below is my code (Movement function, IsGrounded function, and the IEnumerator) if anyone is able to help me deduce the problem here. :)


    It's as if the Space bar input condition is not being fulfilled because my IsGrounded method is not returning true, but I am unable to see the fault in my logic. Maybe I'm just tired! ;(



    Thanks for taking a look and I look forward to figuring this out!

  • If jump is not reading when space is pressed, then you should inspect your isGrounded() method. Your problem will be in Raycast, it could be that it never touches ground or.

    For more cleaner code i would remove "== true" in your if statement in Movement method. so it would look something like this below:


    if (Input.GetKeyDown (KeyCode.Space) && IsGrounded ())

  • If jump is not reading when space is pressed, then you should inspect your isGrounded() method. Your problem will be in Raycast, it could be that it never touches ground or.

    For more cleaner code i would remove "== true" in your if statement in Movement method. so it would look something like this below:


    if (Input.GetKeyDown (KeyCode.Space) && IsGrounded ())


    Thanks for the feedback gobol! I had it that way initially and switched it for testing purposes; forgot to change it back before the code paste.


    I know that the root of the problem is somewhere in the isGrounded() method because when removing it from the space input if() statement, the player responded to user input again. I think I may be overlooking something small here but I can't for the life of me see it today.

  • Hey guys, thank you for the feedback. I was able to isolate the problem by drawing the ray again.


    For some reason by emulating the same settings Jon has, a ray with a length of 0.6f doesn't hit the ground layer in my Unity environment. Increasing the length of the ray to 0.9f fixed the issue. Cheers!

  • It probably happened because of different object size. Glad you've got it working, mark the thread as solved and keep on coding :)

    Already done! Thanks again for helping steer me in the right direction. When I originally posted this I was a bit fatigued and didn't even think about drawing the ray again to actually test if it made it to the ground layer or not. 8o