Hello guys.
Welcome to the video.
In this one I want to show you how to set up jump for the character.
And we're not going to just set up jump.
We also going to set up collision detection.
So player knows when he's on the ground and when he can jump and when he cannot jump.
Okay, the jump functionality itself is very simple.
It's going to be done with the help of input from a keyboard.
And this is probably a good chance to discuss different, uh, type of inputs.
Uh, let's type here if input get key.
Key code space.
Debug log.
Um, holding space.
Then here we're going to type if input get key down.
Uh yeah.
Down.
Key code space we're going to type Debug.log.
I pressed space and then if input.
Get.
If input.
Get key up.
Key code.
Space.
We're going to type.
I'm sorry.
Debug log.
I released space.
So these three ways of doing input have a simple difference.
Get key is called value.
Holding the key get key down is when you press the key.
And then there is a get key up which is called when you release the button.
Let's go ahead and try this.
Just so you know different ways and how it can affect the input.
But we're going to use only one of them.
So in the console I'm going to.
Press the key and I'm going to hold it.
And we can see we have I'm holding space.
I pressed space when I'm releasing the button, I released space.
This is what it does.
Simply simple as that.
Yeah.
Um, now that we know that, let's just use one of them and we're going to use get key down space and
as a functionality in result, we're going to make a jump.
Jump is very simple to do.
First of all, we need to know the jump force how high character can jump.
So I think it can be done here.
Let's do serialized field, private float, jump force.
And I think it would be a good idea to make a header.
So it's a bit more recognizable in the inspector.
Let's type here header.
And in quotation marks we can type uh movement or something like that.
Now if we save this and go to unity.
We're going to see.
We've got.
A new variable and a header.
Movement.
Move!
Speed!
Jump!
Force!
Nice.
Now let's go and apply this jump force.
In the update we can type RB velocity equals to new vector two.
And we don't want to change the x.
So it's going to be rb velocity dot x.
And for the y it's going to be jump force.
Let's go back.
And we're gonna give it a jump force of maybe six.
For now, we just want to test and make sure it's working.
And when I'm pressing space key, I'm jumping.
Yay!
Nice.
Now there is an issue.
If I jump a lot, I'm going to fly.
This is not what we need, right?
We want to jump only once when we are on the ground.
For that, we need to be sure we know when player is grounded.
How do we do that?
Well, first of all we need to define what is a ground in the game world.
For that we can go to layers over here and click Add Layer.
We have an empty field over here.
So I'm going to type here ground.
Very nice.
Now let's select the platform itself and change layer to ground.
Now that we know that this object is a ground and now we just need to detect it and be sure that player
is standing on the ground when we're trying to jump.
How are we going to do this?
Well there is such thing as a raycast.
Raycast is basically a line that drawing from one point to another.
And if this line is detecting anything, then we can say that, well, we detected a ground, for example,
or something like that.
So basically we just can use this raycast which is a simple line to detect if player is above ground.
And.
Let's go.
Over here and make a header.
Header.
Collision.
Uh, info.
And then we're going to make private.
Bool is rounded.
Very good.
Now we're gonna go to update, and in the update we need to type something.
We need to type is grounded equals to.
And here how you make a recast.
It might be a bit confusing especially if you are new.
Don't worry about it.
Just type with me and learn how to use it.
And if you don't understand it quite well right now, you will understand it later for sure.
So let's type here physics 2D dot raycast.
And this will say that we want to do a physics 2D raycast.
And then in the parentheses you can see it says what do we need here to make it work.
So we want to draw a raycast from original position of the character from the center.
So I'm going to do origin transform position.
Then we need direction, and we want to cast a ray from a center of the character down like that.
This is what we want to do, right?
We want to see what is below the character.
If we want to cast it down, we can just simply type here vector two dot down.
And if you see this for the first time, vector two dot down is equals to vector two zero minus one.
Simple as that.
Now if you put your mouse over here it says that that's good.
That's probably everything that we need.
But we can move further and apply more settings to this raycast.
For example we can know the distance.
How far should we do the check?
For that we need to make a serialized field private float.
Ground check.
Distance over here.
Please make sure that you put this line above the boolean, because header will not show itself if private
value is after the header.
Uh, something visible should be after the header for header to work.
Okay, so just make sure you have ground check and then is grounded.
So now that we have this value we can go here and type ground check distance.
If we put a comma over here, we can see that we can use layer mask as well.
Right.
So for Layer Mask here we need to make one.
So let's go back here again and make serialized field private.
And this time we're going to use Layer Mask as a data type.
Layer Mask.
And then we're going to just name it as what is ground.
Simple as that.
And then over here we're going to type what is ground.
So this is how you detect things around the objects.
There are many ways to do detection around the character.
But this one is the simplest for now.
So let's just save it and it will work.
But there is a problem.
Raycast is working there.
It is there.
But we cannot see it because you know it's not visible to human eye, it's only visible to the Unity's
eye.
So if we want to see it, we just need to draw a line that has same parameters as the one that we just
described.
For that, we can use on draw gizmos.
On drawer gizmos does not do a raycast check on drawer gizmos only draws a line itself.
It only shows you something, but it is not the same as this one over here.
It's just that we can draw it in a very similar way, so both of them will represent the same info.
Anyway, let's just type here.
Gizmos draw a line from Transform.position and as an endpoint we need another vector and we cannot just
type it like it was before.
Over here it would be a bit different.
So we need to type here.
Uh, new vector two for the X is going to be not changed at all.
So we're going to type transform position dot.
X.
And for the y we're going to type transform position dot y minus ground check distance.
Very well.
Let's save this.
And let's go back to the unity.
So now if we increase this value over here, we can see a line is drawn and this whole raycast will
work as well.
So we just need to be sure that this line extends outside of a capsule collider.
And that would be enough.
So I think 0.8 is actually a good value here for me.
You can try a different one if you need to, but 0.5 should be fine.
And also we need to choose what is ground.
And that would be ground layer.
Very nice.
Now if we go to play mode.
And go to debug over here.
Sorry.
Let's go to debug.
We can see that is grounded.
True.
If I jump, it will become false.
True, false.
Because we're not detecting ground anymore.
Because we are in the air.
How can we use it?
I think you know how we can use it.
It's a very simple thing.
We go over here where we're trying to jump and we can type here and is grounded.
So if we pressing the key down and if character is grounded, we perform the jump.
Let's save this and let's go back to unity and try this out.
Go to play mode.
And now we jump in.
But if we try to jump while we are in the air.
Doesn't work.
Isn't it cool?
It's very cool, man.
I love it so much.
Now there are some things that could improve.
Feeling of a jump.
Um, that would be very easy to adjust.
We can go to rigidbody and then we go to play mode.
And over here you need to play with the gravity scale and jump force.
Gravity scale makes character heavier.
So if you type here four and try to jump again, look what is going to happen.
He barely jumps, right?
So now we want him to jump higher.
And personally, me, I want to have a feeling where character feels quick and heavy, like he is just
jumping really high.
But it's not that easy to do.
So on the jump force, I'll try to set value to 14.
And actually I know the value because I tried before.
So jump force 14 and Gravity Scale 4.5.
And if we try to jump again, this is how it feels.
And I'm really satisfied with the this feeling for you.
I would suggest to use these values for now and wait for the later setup of the character, because
we still need to set up jump animations, and also we need to make him flip when he goes to the left.
And once we do that, it's going to be a bit easier to adjust and see what the jump force and gravity
scale you should have and how it should work better.
So as of now, let's just leave it as it is.
Maybe let's just keep one and six.
It's okay.
Once we have entire setup, it will be a bit easier to balance.
I think, because you're going to not only see how it feels, but but also you're going to see how it
looks.
And that is very important as well, right?
Yeah.
So probably let's just keep one on the gravity scale.
Jump four six should be fine.
Now, uh, before we end this video, I want to do some changes here.
This feels a bit messy and it can get out of hand very quickly.
So I want to clean this up.
How?
I want to clean this up.
Well, maybe, just maybe, let's make a method.
And this time I want to show you another way of making a method.
You can type here handle collision as it would be a method.
And now we can see it is underlined because there is no such method.
But if you press alt Enter or Ctrl plus dot you will call uh Quick Actions menu.
And here you can choose Generate method.
Very nice.
Now we need to know what will be inside of this method.
And inside of this method I want to place this collision.
So I'm just going to cut it out.
And paste it in here.
Very good.
Now it is a bit cleaner and I like it more.
There is something else I want to do.
I think it would be a good idea to take all of this.
Click alt, enter, extract method, handle input and here we're going to take care of the input.
So now we have everything clean and nice.
The only suggestion I would do here is to take handle collision and move it above.
Because we want to have collision information updated first, then we want to have the input.
Then we want to update the animations and then we want to apply movement.
Actually, I think animations can be after movement because we change velocity and then we apply animation
to that.
So yeah this is how it should be.
I think this is the execution order in the update collision input movement animations.
Um, for better understanding of the code, we could also take this line of code and just wrap it into
a method that would say just jump.
So let's do extract method.
Jump.
It's a bit better and easier to read.
Um, let me see if we have a couple of minutes now.
I think that's good for now.
That's good for this video.
I'll see you in the next one.
Okay.