Hello guys.
Welcome to the video.
In this one I want to show you how to make jump and full animations.
And also I want to clean up our code just a little bit more.
So first let's make the animations for Jump and Fall.
We're going to go to animation Window, select animator and then click Create New Clip on the animations
folder we need Player Jump.
And we need one more.
Create new clip player.
Hold.
Very nice.
Now let's select uh graphics folder over here.
Go to Main Character virtual GUI and take this folder animation just one sprite and place it over here.
Sample rate is not important because we have only one frame here.
Now let's select uh jump.
And place it here.
This would be for jump animation.
Now our goal is to switch between these two animations when player goes up or down.
Luckily for us that is super easy to do and we did something similar with the movement before.
We're going to go to animator and we can just delete these two animation states.
We don't need them.
And over here we're going to make new Blend tree which I'm going to call.
Jump.
Very nice.
Now let's go inside of this jump file.
Select it.
And over here we need new parameter.
We cannot use x velocity for this.
For this one we're going to use y velocity.
So we're going to add float parameter here and type y velocity.
Now let's, uh, go to parameters and choose y velocity here.
And now we need to add two motion fields.
Add motion field.
The one over here going to be player fall.
The second one going to be player jump.
And we're going to uncheck all to my threshold and make minus one and one.
So now we're going to play animation.
And we can see if y velocity is higher than zero.
He's going to jump.
If it's lower than zero he's going to fall.
And we just need to update this parameter in the script.
That's all what we need to do.
And let's go to the player.
And quickly update this parameter.
On the animation, we're going to choose anim set float for the y.
Velocity is going to be our velocity dot y.
For now.
Let's just go there and make sure this is working.
We're going to go back to the unity.
Go to base layer.
Click play.
And we can see we have y.
Velocity is zero.
Now I'm going to try to jump.
And yes we updating the value.
The only goal now is to connect these two states over here, just like we did in the first example.
But this time condition will be something simpler.
We're going to check if he is grounded or not.
And we don't need to do much for that because we already have a collision check.
We just need a bool parameter that would say is grounded.
Then let's make transition from idle.
Move to jump.
Fall.
The condition would be E is grounded.
True.
Has exit time and check transition duration zero.
Now let's make transition back.
This one would say.
Is grounded.
True.
Wait a second.
This was his grounded.
False?
Yes.
And if it's a true, we go back to idle and move.
So.
Yeah.
I'm sorry.
I hope I did not confuse you.
When we go from idle to jump.
We said grounded.
False.
When we're going from full jump to idle move, this should be true.
Transition duration zero has exit time and check for immediate transition between animations.
Now we need to update this parameter in the script as well.
We go back to scripts and over here we type enum set bool.
Is grounded to the boolean we have is grounded.
Okay, let's save this.
Let's go back to unity.
And let's try it out.
I'm gonna jump and we jump in and animation is there.
Jump and fall.
How cool is that?
Super cool.
Love it.
Now let's go back to player script.
And here I want to show you a couple of tricks to make your code sort of cleaner.
This is not mandatory.
You don't have to do it.
I just like to do it.
And I want to show you what I'm talking about.
Okay?
So first of all, when you have just one line of code in the method like I have here, in the jump,
you can remove.
This.
You can remove these curly brackets.
Then you can type over here equal sign and bigger sign.
Together they will resolve what is called a lambda expression.
To put it simple, it's like saying this method will do this for us.
So again our velocity equals to new vector two, our velocity dot x and jump force.
And this would work same as the function before.
But because it has only one line of code we can type it like so.
And this will make it a bit, you know, uh, smaller I would say, because before it took so much of
space, but now it only takes this much of space and we still can see what is inside of this function.
So this is how you do it.
And this still would work.
Uh, let's go and make sure of that.
And working as before.
Very good.
But you can do this only if you have one line of code.
If you have more than one, then you would have to open curly brackets.
Uh.
Place it here and do like debug log.
Another line of code.
Okay.
Yeah, so that's how I want to keep it for now.
And another trick, which is very handy if in the if statement, you have only one line of code, just
like I have here in this example.
Then again, you can get rid of curly brackets.
You can remove this and this and make sure you don't have an empty space between them.
And now this would be the same as we just seen.
But it takes less of a space on the page.
Okay.
And it would work the same as before.
But again if you have more than one line of code, then you would have to extend it to curly brackets
and use it like so.
That's the simple way to put it.
We could also do this over here, but I'm going to introduce another collision check later on.
So I'm not going to do it.
I'm going to keep it open with the curly brackets okay.
For the movement as well.
We're going to add some changes later on.
So let's keep it as it is.
We're good to go.
I'll see you in the next video.