As you can see, I have opened it up an empty project, so we will move over to our hierarchy.
I just want to make the background a little bit dark gray, something like that.
Uh, so I'm gonna select my main camera in the hierarchy.
Let's go over to Inspector Camera component, and then let's click on this background.
And we're gonna choose this color, something like that.
And let's create our player.
Uh, but before we create let's create the ground first.
So we will head over to hierarchy.
Let's right click or hit this plus icon.
Then go to 2D Object Sprites.
And we're gonna choose this square.
And let's select the move tool from this interview.
And let's move it somewhere around here.
And let's select the rake tool from here in this in view.
And we're gonna hold down alt.
So it's gonna resize from center as you can see.
So this is our will be ground.
So let's rename this to square to ground.
round.
So let's call it round and let's add some components to this round.
So first thing I'm gonna add a sum.
Firstly I'm going to add a box collider 2D for collision detection.
So let's click on this add component and let's add a box Collider 2D.
And now let's create our player.
And to do so let's head over to hierarchy.
And we're going to right click or hit this plus icon.
Let's right click let's go to 2D objects sprites and then square.
And I'm going to call this square to something like player.
And and we're going to reset the transform.
To do so let's head over to Inspector Transform.
Let's right click there and then reset.
And we will add some components to this player as well.
First thing we will add a rigid body 2D for gravity.
So let's click on this add component.
Let's add up rigid body 2D.
And let's scale up the gravity is something around three and collision detection to.
We're going to choose continuous and interpolate.
We will choose interpolate or you can just skip this interpolate.
You can just leave it to none.
And if you choose interpolate.
So it's going to make a little bit smooth.
So that's why.
And we want to rotate as z rotation like this to stop this.
So firstly I'm going to set back to zero.
We will move over to constraint.
And then we're going to freeze the rotation about z axis.
And now it's done.
And let's add one more component which will be a box collider 2D for to for collision detection.
So let's click on this add component and let's add up one more time box Collider 2D.
So if we go ahead and then click on this play button in the top you will see this player game object
just gonna fall down as soon as the game just gonna start because we added Rigidbody 2D and later on
we're gonna to replace this square with something player.
So we need to import all of the assets that we need, and we can take a look on it later.
So as you can see, as soon as our game started, this player game object just fall down due to our
gravity to this ground.
So let's get out of play mode and let's make the movement to do so, we will create a script.
So we will head over to our project tab assets.
And then uh, and then this assets folder, let's right click there create folder.
And you can call that folder to something like scripts.
So let's call it script.
And I already created a player script as you can see to create that just move over to that script folder.
Then right click create then scripting, and then click on this Monobehaviour script.
And you can name that script to something like player movement or simply player.
So I have the script, I'm gonna take this and drag it over to that scripts folder to keep the project
a little bit clean.
So we just create folders to keep our project clean, and it's going to take a few moments to compile.
And what we will do now we're going to select our player game object in the hierarchy.
And let's select that player script.
Drag it over to this Add component section and the inspector.
Or you can simply click on this add component.
And you can find the script by typing the name of the script, which is in our case player.
So so we're going to click on this add component.
So we already drag it as you can notice in the inspector.
Or you can simply click on this add component.
And you can find the component by typing the name of the component.
So in our case player.
And then click there in order to attach it.
We're not going to do it since we already attached.
So we will open up our player script by double clicking on it.
So as you can see by default we have the start function and this update function.
So we will go to the top of our class and we're going to make a private float movement.
Uh, basically what we will do, we will move over to this update function.
So we do know that this update function runs every single frame.
So every single frame we're going to check for some input.
So to move our player left and right we're going to use this input dot get access function.
So this input dot get axis.
And then make this first bracket.
And inside this first bracket we need to put the axis in which axis we want to move.
Since we want to move our player horizontally.
Uh like this.
So in quotation we need to type horizontal and make sure you spelling it correct.
So put quotation.
And inside this quotation you need to put this horizontal horizontal and then close that output semicolon.
So this input dot get axis horizontal function.
It's going to check some input.
So let's suppose you press down press down D key on your keyboard, or you hold down D key on your keyboard.
Then this function over here is gonna give us one.
And if you hold down a key instead of D key then this input z axis horizontal is just gonna give us
minus one.
And if we don't hold down both of those keys it will be zero.
So this input or gate is just going to check for with some input which will be a and D.
If we hold down a key then it's gonna give us minus one.
And if we hold down D key it's gonna give plus 1 or 1.
And if we just lift off our finger from both of those kids, then it's gonna give us a zero value.
So what we will do, we will store that value into a container or into a float variable.
Since this is gonna return one or plus 1 or 0, which will be a float number or integer, but it would
be perfect if we just store into a float number.
So what we will do?
We will go to the top of our class and we're gonna make a public variable.
But it doesn't need to be public so we can make it private instead.
So if we make it private, we do know that we're not gonna be able to inspect underneath or basically
we're not gonna able to see underneath our player skip in inspector.
So but we still want to see.
So we're gonna tweak that.
So let's make a private and that will be float.
And we can call this one movement.
So let's call this one movement.
And let's close that up with semicolon.
And now what we're gonna do whatever we get we're gonna store that value into this movement parameter.
Or you can say a variable or variable or you can see this float movement.
It's just gonna work as a container.
So we're storing the value into this movement.
So let's suppose we hold down D key then this input dot get access horizontal function is going to give
us a plus 1 or 1.
So this movement right over here is going to be plus 1 or 1.
Because as you can see it's simply we just simply setting it movement equal to input dot get axis horizontal.
And let's assume if we hold down a key then this input dot get axis horizontal function.
It's gonna give us minus one.
So that's why this movement right over here it's going to be minus one.
And if we lift off both of fingers from both of those keys then this input dot get access horizontal
is gonna be zero.
So that's why this movement right over here will be zero.
So let's now press Ctrl S in order to save our skip.
And now let's move over to unity.
So we will see that movement just gonna change according to which cube we are holding down.
If we hold down D key, then it's going to give us plus 1 or 1.
And if we hold down a key, it's going to give us minus one, minus one.
But the problem is that, as you can see, as soon as our player skip gets compiled, we are not able
to notice our movement parameter underneath our player skip.
Why?
Because we made this field private, as you can see.
But let's suppose we still want to see that field underneath our player skip.
So how we can do that.
In order to do it just move over to Inspector.
In the top right you can see three dots.
So just click there and then click on this debug.
And as soon as we have chosen debug we can see underneath our player skip the movement.
And by default it's set to zero.
So now if we click on this play button in the top and try to hold down a and key, you will see this
movement parameter just gonna change with the key we holding down.
If we hold down D key then this movement will be one.
And if we hold down a key this movement will be minus one.
So now if I hold down D key, as you can see, as soon as I am holding down it became one.
And if I lift up my finger it became zero.
And now if I hold down a key, it became minus one.
And if I lift up my finger it became zero.
So I hope you get the idea that how how we can use that function in order to move it.
So I'm going to get out of play mode.
And now what we have to do, we need to move our player instead of just making this minus one or plus
one.
So if we just are, let's suppose you just go or want to go back to normal mode.
So just move over to Inspector one more time and then top right you can see three dots.
So just click there and then click on this normal.
So it's you're going to go back to normal.
So what we're going to do we're going to use that movement parameter in order to move our player.
So if we select our player in the hierarchy and then move over to Inspector and then transform component
and play around with X position, we can see that this player is moving left and right.
So we need to replace this x position with our movement parameter, which can be minus one plus 1 or
0 depending upon which key we are holding down.
So if I hold down d key so then if we replace this x with our movement.
So movement is just going to be one.
And as soon as we put in one this square or this player just going to the right direction, and if we
hold down a, a a key, then we do know that that movement is going to be minus one.
So if we put minus one we can see this player is moving to the left direction.
So we're gonna use that parameter in order to move it.
So I'm going to set back to zero.
So what we need we need to move over to this transform then position.
And top of that we're going to add a Vector3.
Since this position has x axis, y axis and the z axis.
And that Vector3 also going to have x axis, y axis and z axis.
So now let's open up our player script by double clicking on it.
And for that we're going to use fixed update.
So we will use this fixed update instead of update.
So this function.
So this fix update function get call with fixed frame rate.
Basically let's suppose in one second it's run 60 frames as 60 frames.
Then in the next second is still going to run in one second 60 frames.
So the frame rate will be constant.
It's not going to be something like 55, sometimes 60, sometimes 62, but the frame rate will be constant
if it's uh, if it's run in one second 60 frame 60 frames means in second, second, it will still run
in 60 frames.
So basically that is the thing.
So that's why we're using this fixed update.
And now what we're gonna do, we're gonna go over to our transform dot position and transform dot Transform.position
over here basically means current position of this player.
Basically we just going over to this transform and then position and top of that we're going to add
our movement.
So we're going to use this plus equal new Vector3 Vector3 which is going to have x axis y axis z axis.
And now we're going to close that up with semicolon because this transform.position have x axis y axis
and the z axis as you can see.
So that's why we just using this plus equal a new Vector3 Vector3 which also has x axis, y axis and
the z axis.
So we don't want to move at at all in the y axis basically up something like this or in the z axis.
So that's why sorry.
That's why in the uh in the y axis we're going to put zero in the z axis.
We're going to put zero f.
But in the x we do one do one move.
So that's why we're gonna pass this movement, which can be minus one plus 1 or 0, depending upon which
key we folding down.
So let's suppose we hold down D key.
Then this movement right over here is going to be one means in the x we replacing our x.
Basically this x with our movement which is one.
So it's going to go to the right direction.
And if the movement is minus one means we holding down a key, then we replacing this x with our movement
which will be minus one.
So this player will start moving to the left direction.
So I hope you get the idea.
And now what we're gonna do, we're gonna multiply this whole thing with our fixed delta time to make
frame rate constant.
So we're going to multiply with time dot time dot fixed delta time.
Why we multiplying with this time dot fixed delta delta time instead of time delta time.
Because we're doing inside this fixed update as you can notice.
So that's why we're using this fixed delta time, which is simply multiplying with time.
Dot fixed delta time.
And now we're going to press down control S in order to save our script.
And let's move over to unity.
So we will start moving to the left or right direction depending upon which key we holding down.
So we're going to take a look on that.
And later on we're going to replace this square with our player.
Uh don't worry about that.
So now if I go ahead and then click on this play button in the top in order to start our game, we should
be able to notice it.
So just wait a moment.
So now let's start our game.
And if I try to hold down D key, as you can see, as soon as we holding down D key it's started moving
to the right direction.
And if I hold down a key we can notice that this player or this square just started moving to the left
lab direction.
But if I lift up my both of my finger from both of those key, we can see player just standing at one
point.
Because if we just, uh, don't hold down A or D key, that input dot get access function is just gonna
give us zero.
Basically means we're not gonna move at all, and we're just gonna stand at a point.
Uh, so I hope you get the idea that how you can do it.
But it would be nice if we move a little bit faster.
So we will get out of play mode and we will open up our player skip, and we will multiply this whole
thing, uh, which becomes minus one.
So what we're going to do, we're going to multiply with some unit.
Uh, so let's make a variable.
So we will go to top of our class.
And we're going to make a variable for it.
And we're going to make this variable private.
And that will be float.
And we can call this one move speed.
So let's call it move speed.
And by default I'm going to set equal to something like five F.
And let's close that off with semicolon.
But as you can notice, we set it private.
So we're not going to able to notice underneath our player skip.
But we still want to see that underneath our player skip.
So we can adjust right over here.
And we don't need to come to the script and change that value over again and again.
But we made it private.
So that's why we're not going to able to notice.
But let's suppose we still want to notice that.
Or we just want to inspect, uh, underneath our inspector tab.
So we need to mark this as a field.
Basically you need to make this square square brackets.
And you need to type this serialized field.
And now even through this, uh, this, uh, parameter is a private, we're still gonna able to see
underneath our player skip.
But the problem is that we're not gonna able to access this parameter from another class or from from
another sea scape.
Basically that is the thing.
And making this way keeps our coat a lot more clean, so that's why.
So we just marked as realized fill.
So even though it's private we still just gonna able to notice underneath our player skip right over
here in the inspector.
And now what we're gonna do, we're gonna multiply this whole thing with our, uh, with our this move
speed.
So let's multiply with our move speed.
Sorry.
Not movement.
We're gonna multiply with move speed and move speed.
By default, we set it to five, as you can see.
So this movement is gonna be minus one, 1 or 0.
So this will be let's suppose we hold down d key.
So this movement right over here it's gonna be minus one.
So this move speed is five.
So minus one into five will be minus.
Uh.
Sorry.
Uh, sorry.
If we hold down d key, I really apologize if we hold down d key.
So this movement is just going to be fun.
And this speed is five.
So five into one will be five means we will start moving to the right direction with a five unit of
speed.
So basically that is the thing.
And let's suppose we hold down a key.
Then this movement right over here it's going to be minus one.
So this over here is minus one.
And this move speed is five.
So five into minus one will be minus five means we will we will start moving to the left direction with
minus five unit.
So I hope you get the idea.
But we do know that we just marked as cereals fill.
So we don't need to calm this, come to this script and change this value over again and again since
we can do it underneath our inspector tab, underneath our player Skip.
And now we're going to press down Ctrl S in order to save our skip.
And now let's move over to unity.
So underneath our player Skip we will able to notice that film which is our move speed and we can just
adjust underneath our player skip.
We can just make it a little bit faster.
Basically, we just multiply it to move a little bit faster.
And as you can notice, as soon as our players kipchoge's compiles move speed, we can see by default
we set five.
And now if we go ahead and then click on this play button in the top and try to press down A or D key.
You will see this player will start moving to the left or right direction, depending upon which key
we holding down.
So now if I hold down D key, as you can see we're moving a little bit faster basically with five unit
to the right side.
And if we uh if we press down a key, we can see we started moving to the left direction with minus
five unit.
And we can increase or bump up the value as well while we doing in play mode.
But make sure you just get out of play mode and then paste that number once again, because whatever
you're going to do inside play mode, it's not going to be saved at all.
So let's suppose if I move over to my player script and then set this move speed to something like ten,
we can see we're moving more faster.
But if we get out of play mode, uh, as you can see, as soon as we are out of play mode, uh, this
move speed is set back to five.
So whatever you're gonna do, it's not gonna be saved at all.
So make sure you just remember those things.
Or you can simply just right click on this, uh, player script and then click on this copy component
while you are in play mode.
And once you get out of play mode, then you can simply right click on this player script and then this
click on this paste component values.
So it's gonna set back.
Or you can simply remember that number as well.
So I hope you get the idea that how you can do it.
And now we're going to turn this player into a prefab.
So to do that we will head over to Project App Assets.
So let's right click on this Assets Create folder.
And we're going to call this folder to something like prefabs.
So we're creating folder to keep our project clean.
So and we're going to select our player in the hierarchy.
And let's drag it over to that prefabs folder inside a prefab.
So to get our player into a prefab so we will have the copy.
And if we just lose our player in the hierarchy we can just restore it from that prefabs folder.
Or you can press down Ctrl Z to undo anything that you really just want.
If you lost something mistakenly, mistakenly, you just lost.
So you can just press down control plus state to undo anything and we will press down Ctrl plus S to
save our project.
So I hope you get the idea that how you can make the player movement.