Hello guys.
Welcome to the video.
So in this one I want to make a buffer jump functionality and to understand it better.
First I want to show you what it does because it's quite confusing actually.
But it is very helpful.
I already made it and I'm going to erase everything and show you how to make it from the start.
But first I want you to see it.
Now look how this works.
I can jump and when I'm falling I can press the jump key.
But if I press the jump key before I land it, this will mean that I pressed the key too early and character
will land on the ground.
He'll lose his velocity and he'll jump again.
Only when we press the jump button again.
But buffer jump allows you to sort of save the input of a space key, and it will bounce off the ground
automatically if you press the button just before you land it.
Something like this will do.
You can see I'm pressing button earlier than I landed.
But I bounced off the ground.
So this one helps to not lose, you know, the velocity and movement and fluent lessness of the movement
of the character.
And also it feels like you are really skilled player and you just press the jump button at the right
time just before you land it.
And that's why you are hopping like that.
But actually it's just a functionality that we made.
You can see how cool it looks and I'm telling you, it feels cool as well.
It feels like I'm doing something right and I just know the character so well.
But actually, it's just, you know, the scripting there helps us do it.
This is how it's going to be if I disable this functionality, you can see how I am not always timing
it, right?
And I'm still a bit running on the ground even I press the button buffer jump allows you to do that.
Hope that I just showed you.
Now let's do it.
So for the buffer jump, we need to know the window during which character still can jump after the
button was pressed.
For that, we need to save the time when button was pressed.
Luckily for us, there is such thing as time dot time, which tells you time in the game and we can
make a variable like a float variable, and then we can save time in the game into that variable.
Let's make a header.
Buffer jump.
And let's make a public for now.
Public float.
Buffer jump pressed.
So when buffer jump was pressed okay.
Now let's go somewhere here I'm going to make a method private void request.
Buffer jump and this method will check if we are in the air is airborne.
Then buffer jump rest equals to time dot time.
If you put your mouse over this time dot time, you can see it says the time at the beginning of the
current frame in seconds since the start of the application.
So basically it's just time in the game and we saving this time to this variable.
Let's go to the space input over here.
And let's request a buffer jump here I'm going to save this.
And we're going to go back to unity and see it in the action okay I'm going to just jump.
And we can see we updating value over here when it was requested.
Here is the value.
So it was requested at 11 seconds of time in the game.
Now we can use this value to activate the buffer jump.
We just need to see if the time that was saved.
Plus the window for the buffer jump is bigger than the current time in the game.
Let's go to the player script and let's make a method.
Private void.
Buffer.
Attempt.
Buffer jump.
Now we need the window.
We need to know for how long we can do the buffer jump, like, during the, uh, couple of seconds
or less than that.
So we're going to do sterilize field.
I'd float buffer, jump window.
And actually I know that good value is point 25.
So I'm going to keep this value.
And this now can be private.
We know what it does.
It's going to have the time when button was pressed.
And now let's go back.
To that method.
Where is it?
Attempt buffer jump.
And here we're going to see if current time in the game is less than time.
When button was pressed.
Plus, the window we created means we still have time to do buffer jump.
And if that's the case, we're going to call jump method and we want to take the buffer jump pressed
and sort of reset it.
We're going to set it to something like zero maybe.
And if we do this then this will not be bigger than time dot time anymore.
And this will work only once.
So it's sort of like a boolean.
Now let's go to the function where we do landing handle landing.
And over here we're going to do attempt buffer jump.
And to make it easier to test I'm going to disable double jump for now okay.
Let's save this.
Let's go back.
Let's go to play mode.
And I'm going to jump.
And I can do the buffer jump.
If you feel like this is too easy to do, you can decrease the value over here to something like 0.1
and it's going to be a bit harder to time the buffer jump, but still possible.
And you can hop like that around.
He doesn't mean you must have this functionality.
It's just one of the ways to improve feeling in the platformer game.
Uh, yeah.
So I'm going to bring back the double jump.
There is this glitch.
When character jumps off the ground right at the start, it is very easy to fix.
We just need to go to the player and make buffer jump activated equals to minus one.
Something like that.
So it will never be true at the start.
Let's make buffer jump window 0.25 and we're good to go.
Thank you for paying attention.
I'll see you in the next video.