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!