Creating An Advanced 2D Player Controller Without Any Unity Components! (Part 4)
Today I am adding jump-queueing and coyote-time to my 2D player controller. Explanation inside ;)
Today’s Objective: Add the minor functionality of jump queueing and coyote time (cant think of a better name) to my 2D character controller.
If airborne and the jump input is pressed within 0.1 seconds prior to landing on the ground, queue a jump and automatically jump again as soon as the player lands.
This is a minor effect, but makes the gameplay feel so much smoother. No more accidental early presses resulting in the character just landing and stopping. Keep the flow going!
If the player falls off an edge (without jumping) and they press the jump input within 0.1 seconds of leaving ground, they can jump instantly as if they were still on the ground. No more accidental late presses resulting in falling to your doom! Keep the flow going!
Both of these effects are pretty hard to visually demonstrate, so bear with me and you’ll certainly feel the effects in the end result.
Jump Queueing Code:
I need 2 variables: One for the time before landing in which a jump can be queued, and one to record the last jump input to determine whether it is within that period upon landing.
The logic happens while not grounded, so the following code goes in the AirborneBehaviour method which is called every frame if airborne through Update.
Now the preparation is done, next is to check for a queued jump when the player lands. This happens in the event triggered upon landing, since it only needs to happen then.
Now it is very easy to maintain a continuous series of jumps, even if you press the jump key a little too early.
Coyote Time Code:
Create 2 variables for recording and comparing the time.
When BecomeAirborne is called (whenever the player leaves the ground):
Now for the jump checks. Since the player is airborne, we go to the AirborneBehaviour method I created in yesterday’s post.
Now the player has a slight “grace period” after falling off something, within which, they can jump as if jumping from solid ground.
With both the jump queueing and the coyote time, you can modify the time as needed. Maybe you want more or less time available? It’s up to you.