Let's add up the acceleration to our player game object.
So over over time we just want this player game object to increase speed.
So let's do that.
So we're going to select our player GameObject in the hierarchy.
And let's open up the player script in Inspector by double clicking on it.
And let's uh what we're going to do we're going to add top of this bit.
So as you can speed by default uh, by we set underneath our player skip which is 4.2 as you can see.
So over time we just want to speed up our player game object.
So we're just going to add top of this speed parameter some value.
So over time just we can increase our speed.
So let's do that.
So we're going to move over to script.
And we're going to make a another variable.
And we're going to make that one as well public.
So we can adjust through inspector as well.
And that will be float.
And we can call this parameter to something like acceleration.
So this will be acceleration.
So I'm going to call it acceleration.
Elevation and by default, if you just want to give a default value, then you can just set equal to
any value you would like to go with.
But I'm gonna give something like, uh, a 0.1, something like that.
And if and close that up with semicolon and what we're gonna do now.
So before we move our, uh, player game object, we're just gonna add top of this speech.
So we're going to type this speech.
So this is our speech.
And top of this speech we're just gonna add up this acceleration.
So we're gonna use this plus equals.
So you need to use this plus equal in order to add top of this speed parameter some unit.
And we're going to add this acceleration.
So we're going to type over here acceleration.
And let's close that up with semicolon.
And to make this number constant what we need to do we need to multiply with time.deltatime as we have
done over here as you can see.
So we're going to multiply this acceleration with our What time?
Total time to make our acceleration constant over time.
So we're going to multiply with time.
Time dot delta time.
And that's it.
So speed uh speed.
We set underneath our player script which is 4.2 as you can see.
And top of that.
So this one is 4.2.
And top of that we're just adding the acceleration and acceleration.
By default we set 0.1.
So 4.2 plus 0.1.
So over time and we do know this update function just going to run every single frame.
So every single frame we just adding top of this speed this acceleration as you can see.
So basically this is the thing in order to uh in order to increase our speed and we're going to remove
this comment.
We don't need double forward slash basically means comment and you can just remove that.
And it's not going to interrupt our code anyways.
So we can just remove that or we can just leave it like that.
It's not gonna interrupt our code.
and we're going to press down control S in order to save our skip.
And let's move over to unity by now.
So over time we're going to able to see this speed parameter.
Just going to just going to increase over time.
So just let's have a look on this.
So just wait for a little moment and you're gonna able to see that.
So by default we set the acceleration point one.
But you can go with any number you would like to go with.
But I'm gonna leave with a 0.1.
As you can see, as soon as the script just gets compiled we can see our underneath player skip acceleration.
By default we set 0.1.
So let's click on this play button in the top in order to play our game.
And let's have a look on this.
So over time this speed right over here is just gonna increase.
And you will see that.
So just wait for a little moment in order to, uh, start our game.
And we can see underneath our player skip in Inspector Speed just getting increased.
So I'm going to click on this pause button in the top in order to pause my game.
And now if we move over to player skip.
As you can see speed by now is five.
So over time the speed just increasing and increasing by this amount acceleration as you can see.
So I'm going to get out of play mode.
So just click on this play button in the top in order to get out of play mode.
And now we're going to make our player jump.
So let's do that.
So we're going to select our player game object first in the hierarchy.
And let's open up the player skip by double clicking on it.
So what we're going to do now we're gonna uh so what we basically want to do if we press down space
key on our keyboard, then we just want this player game object to jump in the y axis.
So when we press down space key.
But you can check for any key that you like.
So we're going to use if statement.
So let's move over to Skip.
And we're going to do it over here in update function.
Because every single frame we need to check if we press down space key or not.
So that's why we're going to do inside this update function.
So we're going to do some more over here in this update function.
And we're going to use this if condition.
So type this if and make first bracket and some curly brackets.
And you need to put a condition in this first bracket condition that you want to check for.
So in this if condition in this brackets we need to pass a condition which should be totally true.
Something like we can simply say five is greater than one.
Is it true?
Yes.
The condition we put in between this bracket and between this bracket is totally true.
So whatever we're going to write in between these curly brackets by now, it will be executed.
So let's suppose we just want to print out a message.
Something like we're going to say five is greater than one.
So let's print out a message.
And we do know that in order to print out a message we're going to type this debug dot log function.
So let's type debug dot log and make first bracket.
Then close that up with semicolon and you need to pass a string.
So make sure you do in this quotation.
So type the quotation and you can pass the message you just want to see in your console tab.
So I'm going to say something like this condition this condition is totally true totally true true.
And make sure you just close that up.
Semicolon.
So we put a condition in this a condition as you can see five is greater than one.
So the condition over here we put in this first bracket is totally true.
So whatever we're gonna do inside this curly bracket is just gonna execute.
So this line, uh, so this over here, this message just should be pop up in our console tab.
Why?
Because the condition we put in this, if it's totally true.
So that's why.
So we're going to press down Ctrl s to save our script.
Now let's move over to unity.
So we will see.
So whenever we're going to click on this play button at the top to play our game, we will see as soon
as the game's gonna start, that message is going to pop up in the console.
Says, this condition is totally true, so just wait for it to compile.
And now if I select the console tab from here, and let's click on this play button in the top to play
our game.
And let's have a look on this.
So the message should pop up right away.
Says this condition is totally true.
So just wait.
And as soon as the game started we can see the message just getting pop up in the console says, this
condition is totally true, so I'm gonna get out of play mode.
So why this message?
Just getting pop up every single frame?
Because we're doing inside update function and we do not update function runs every single frame.
So every single frame, uh, this line of code just getting executed basically means this message just
getting pop up in the console every single frame because we're doing a update function, as you can
see.
So instead of checking like this and let's suppose you put something like one is L, one is greater
than five is this condition is true.
No, this condition is totally false.
So that's why in between these curly brackets, by now, you're gonna put it not gonna be executed at
all because you put in this the condition, which is totally false.
So we need to put a condition which should be totally true.
So what we're gonna do, we're gonna check for some input.
So we need to use this input dot, get, uh, key function and then make this first bracket.
And this bracket you need to pass the key code you just want to check for.
I'm going to check for space key.
And basically once I press down space key I want this player game object to jump.
So I'm going to check for space key.
So let's go to that line of code.
So we're using this input dot get key function in order to check for input.
So you need to put in this brackets a the key a which uh With which key you just want to check for.
So I'm going to check for my space key.
So I'm going to pass Keycode key code dot space.
Key code dot space.
It basically means we just pressing down space key.
So if input dot get key key code dot space basically means if we press down space key then this line
of code just going to be execute.
You can check for any key that you like.
So you can check for this a as well as.
So it's going to check for if you press down a key then what's a whatever you're gonna do inside this
curly bracket.
It's just gonna execute.
So I'm going to check for my space key.
So that's why I'm going to pass a key code dot space.
And what I'm gonna do now I'm going to make a function in order to jump.
So I'm going to make it somewhere over here.
And for function what we basically need to do, we need to type this void.
And we have to name the function what we want to name it.
So I'm going to call it something like jump.
And then you need to make this first bracket and some curly brackets in order to make the function.
So for function you need to type void and you have to name the function what you want to name it, and
make sure you just make this first bracket and some curly brackets.
And let's suppose you just didn't assign it to something like public or private.
So this function automatically by default it's going to be a private function.
And let's suppose you just put public.
So it's gonna be public function.
Or you can just make this one as well private.
And and if you just remove this private or public it's gonna be automatically by default gonna assign
a as a private function.
And now what we, I want to do in this function or inside this curly brackets, I just want to print
out a message for now.
So I'm going to type Debug.log in order to print out a message and make first bracket.
Then close that up with semicolon.
And you need to put a string that you just want to see in your console tab.
So I'm going to say something like player jump, player jump.
And right now what I'm going to do I'm going to remove this message.
Message from this if condition.
And what I'm going to do I'm going to call this jump function.
So we just made the function.
We also need to call this function in order to execute this line of code.
So we're going to call the function inside this package.
So we're going to call the function over here which is in our case jump.
And you need to make this first bracket and then close that up with semicolon.
So let me explain what we basically doing right here here.
So we're just using this if condition in order to check.
And we're checking if input dot get Kiki code dot space basically means if we press down space key on
our keyboard, then what we do is we're just calling this jump function and jump what we have done inside
this jump function, we just printing out a message in the console says player jump as you can see.
So as soon as we're gonna press down space key on our keyboard, this jump function And this jump function
over here we can see is just printing out a message in the console says player jump.
As you can see.
So we're going to press down control S in order to save our skip.
And now let's move over to unity.
So whenever we're going to press down Space key on our keyboard, you will see that message is going
to pop up in the console says Player Jump.
So we're calling that function jump function.
And we do know what we're doing inside that function.
We just simply, uh, just, uh, simply just printing out the message in the console says player jump.
So let's have a look.
So I'm going to click on this play button in the top to play our game.
And once I press down space key on my keyboard that message is just going to pop up right away.
So just wait for it to compile.
I'm really sorry for the noises.
So if I press the space key, as you can see, as soon as I press down space key in the console tab,
we can see a message just getting pop up, says Player Jump.
So instead of printing out what we just wanted to, we just want to make clear jump.
So let's get out of play mode, and let's double click on that message in order to move that line of
code.
And instead of printing out message what we want, we just want to make our player jump.
So before we make our player jump.
So if we head over to unity and then select this player game object in the hierarchy and press down
F in order to focus on that game object.
What we need we need a reference of this rigidbody 2D component of this player game object.
Then we can make our player jump.
So basically what we need if we select our player game object then go over to Inspector.
We can see we just added up a rigid body 2D component to this player game object.
And this rigid body 2D component is responsible for velocity, adding force, and lots of other things.
So we need a reference or we need a link of this rigid body.
Then we can make our player jump.
So what we need we need to grab the reference of this rigid body of this player game object in order
to make our player jump.
So let's move over to our code and let's go to top of our class.
And we're going to make it public so we can see underneath our player skip basically over here.
And the component we're looking for is a rigid body 2D.
So you need to type the component that you're looking for.
So in our case rigidbody 2D.
So we're going to type rigidbody 2D.
And we have to name the component what we want to name it.
So I'm going to call it something like RB and then close that up semicolon RB short for basically rigidbody
2D.
So we just made a variable rigid body type.
So so we're going to able to see this slot or this parameter underneath our player.
So we're going to press down Ctrl S to save our script.
And let's move over to unity.
So underneath our player we're going to able to see that rigid body slot.
So what we have to do we need to drag this rigid body component into that slot.
As soon as this player skip gets compiled, we can see underneath our player skip, a slot just pops
up and it says none Rigidbody 2D.
Basically, it means empty.
So what we need to do now, we need to select this rigidbody from this player game object.
So make sure you select the player GameObject first in the hierarchy.
Then move over to Inspector.
And we're going to take this rigid body of this player game object and drag it into that slot.
And now we can see we have player rigid body reference.
As you can see means we have the reference of this rigid body 2D component.
And now we are able to make our player jump.
So let's open up our player script by double clicking on it.
And let's move over to this jump function.
So we're going to remove this message.
Instead of printing out message we just want to make our player jump.
So what are we going to do.
We're going to go over to RV.
Simply we're just moving over to rigid body.
And then use this dot add at first function in order to add the force and then make this first bracket.
So this rb dot add force function.
It's going to take the direction in which direction you just make one.
You just want to make the jump.
So we just want this player game object to jump in the y axis.
So if we select our player GameObject in the hierarchy and then move tool, we don't want this player
game object to jump in the x axis.
Something like this.
We want this player game object to jump in the y axis.
So we're going to use a vector tool because that vector two is just going to have x axis and the y axis.
So we can just set y or x according to what we really need to do.
So let's go to that.
Uh let's go to script.
And we're going to use this new vector tool and sorry new vector two.
And we do know uh, so make some uh first bracket.
And this vector two is just going to have x axis and the y axis.
So we don't want to make our player jump in the x axis.
So that's why in the x axis we're going to put zero f and in the y.
We just want to make our player jump some units something like 5 or 7.
So let's make another variable for our player jump.
So let's go to the script and let's go to the top of our class.
And we're going to make another variable.
So let's make this one public so we can adjust underneath our player skip.
And that will be float.
And we can call this one something like jump height.
So let's call it jump height.
And by default you can give any value you like.
So I'm going to give something like seven.
And let's close that off with semicolon.
And if you just want to do just want to close that off something like this, then you can adjust underneath
your player skip as well.
But I'm gonna give a default value.
So that's why I'm going to set equal to seven.
And now what I'm going to do in the Y I'm going to pass this jump height and the jump height.
By default we set seven as you can see.
So this just gonna be seven means our player GameObject will jump in the Y axis with seven unit, something
like this.
So this rb dot add force function is.
It's basically a force.
So that's why as a comma.
So you need to put this comma as a second argument.
You need to pass this force mode to d dot impulse.
Why.
Because this is a force.
So that's why as a comma you need to pass the force mode to d dot impulse.
So firstly we just pass the direction.
And the second thing we need to pass the force mode.
So we're going to type force mode 2D dot impulse and then close that up with semicolon.
And we're going to press down Ctrl s in order to save our code.
So let me explain one more time what we basically doing.
So let me explain that.
So just wait for to compile.
And once our script gets compiled we are ready to do that.
So what we're doing firstly we're just going over to this RB basically means we have the reference means
we just moving over to this player game object this is rigid body 2D, and we're using our force function
in order to add the force.
And we do know it's going to take the direction.
So in the Y we set jump height as you can see.
And in the x we set zero basically means we don't want to jump in the x axis.
We only want to jump in the y axis.
So that's why we set a.
We just replace that y with some height and jump height.
By default we set seven and we can see as well underneath our player skip.
And we can adjust it over here as well.
So that's why we just made that field public.
And make sure you just press down Ctrl s in order to save your skip.
And now let's move over to unity.
And let's click on this play button in the top in order to play our game.
And let's have a look.
So whenever we're going to press down space key that jump function just gonna call.
And we do know what we're doing inside that jump function.
We just making our player jump.
So if I just press down space key as you can see.
So I'm gonna get out of play mode.
I just want to make the player speed a little bit low so we can just test the jump.
So I'm going to make this speed parameter underneath my player skip something like one.
And now let's click on this play button in the top in order to check that jump.
And you can adjust the jump height as well underneath your player skip.
It's totally up to you.
So now if I press down space key on my keyboard, as you can see, as soon as we press down Space Key,
we are able to see this player game object just jumping in the y axis.
And and that is cool thing.
I hope you get the idea.
So I'm gonna get out of play mode and make sure you just press down Ctrl s in order to save your project.
And also make sure you select your player game object in the hierarchy.
Then go over to Inspector, then overrides and then click on this Apply all.
If you made your player game object a prefab, so make sure you just do that.
Or you can just skip this part as well if you haven't made this player game object prefab.