How to deal with grounded check after jump initiated?

Hi, I’m looking for some advice on how to handle a scenario I’ve run into involving jumping and grounded checks.

Some quick background - I’m working on a 2D platformer and I’m fairly new to Unity, but I’m an experienced developer and I know C# well.

I’m using a RigidBody2D on my player entity. I am using two raycasts to determine if the player is grounded, basically one on each side of the player sprite.

I have numerous custom Monobehaviors attached to the player. One holds some state info, like “isGrounded”, “isJumping”, etc… and the others are for handling the different types of actions the player can perform - one for horizontal movement, one for jumping, one for attacking, one for wall jumping / sliding, one for grounded checks, etc…

Here is my scenario - In my Jumping MonoBehavior, when I press the Jump button I apply positive Y velocity to the RigidBody2D and I set the “isJumping” bool to true. My problem is that almost immediately after setting “isJumping” to true, it gets set back to false.

This is because in my Jumping MonoBehavior I have code that says If isGrounded = True then isJumping = false. Basically at the start of the jump my “grounded” raycasts are still colliding with the ground, the player hasn’t achieved enough height between the jump being initiated and the next isGrounded check.

I need this “isJumping” state set properly as I use it to drive behavior for stuff like double / multi jumping, wall sliding / jumping, etc…

So I’m looking for advice on how to handle this. Is there some common practice / pattern that is used to handle this scenario? Or maybe I am going about this all wrong and there is some better way of handling jumping / grounded checks that completely avoids this scenario?

Any help / advice would be appreciated. I could post relevant parts of my code if that would help, I help off for the time being since this is more of an advice / approach question as opposed to a technical question. Thanks!

Hey! @cjf420 @aksaini136
I have a solution for that.

Try something like this.

if (_rb.velocity.y == 0 && _isJumping && IsGrounded())
{
    _jumpCount = 0;
    _isJumping = false;
}

The issue is if you don’t add the velocity check here, your ground detection might last an extra frame or two it takes to update the RigidBody position and reset your values prematurely.

Hope this solves your issue!

Hi @cjf420 , I am facing the same issue, did you find a fix for this?

Physics.Raycast has a max distance limit…you can set it to an appropriate distance depending on where your raycasts start… however, i am probably too late…

The best solution I found was to set a timer for being grounded, so when the character goes from grounded to not grounded there is a small cooldown before the character can be grounded again.

Running into the same issue, how did you go about adding a count down timer in the code?