Let's move our player game object.
We just want this player game object to move in the right direction.
So we're gonna create a script for that.
So what you basically need to do you need to select the player game object in which game object you
want to do.
So in our case player.
So we're going to select our player GameObject in the hierarchy.
Then go over to Inspector in the right.
And then click on this add component over here.
And in the bottom you can see New script.
So just click there, name the script or whatever you want, something like player movement or player.
And after doing that click on this create and add or else what else you can do.
You can head over to this project tab, then assets and then create a script folder.
First something like right click first, then create folder and name that folder to something like scripts,
and then move over to that scripts folder.
Then right click or then create in the top, then monobehaviour script.
Or you can just scroll down a bit which is say which says scripting and choose this Monobehaviour script.
Not anything, not others, because we're going to attach this script to our game object.
So as you can see, I already have created.
So what I'm going to do, I'm going to select my player GameObject in the hierarchy.
And let's select that script and drag it over to this Add Component section in the inspector or or you
can just simply do this.
Click on this add component and you can find the script by typing name of the script.
So in my case player.
So as soon as I type you can see the script just gets pop up.
So I'm not going to attach it twice because I already have attached as you can see.
So I'm going to open up our script.
So let's double click on this player script over here to open that up in Visual Studio.
So by default as you can see we have start and update function.
And it's called class player.
Basically class is a template.
We're gonna use this template in order to move this player game object.
So you can think of it.
This this is the design and we're gonna implement in this player game object.
Let's suppose we just want to move this player game object to the right direction.
So we can define those attributes in order to move this player game object in the right direction.
So this is the class.
And we're going to use this class in order to move our player game object.
So this is the thing and we have start function over here.
So void basically means function or method.
You can simply say and this is start function.
And start function gets called when the game start.
And this update function over here is just called again and again and again.
So let's suppose you want to check.
So we just want to see a message in the console.
So if this function correctly working or not.
So let's go to start function in between these curly brackets.
Whatever you gonna write.
It will be executed in the start of the game.
Why?
Because this start function get call in the start of the game and it just call one time.
And let's suppose you just want to see a message.
And in order to see a message or just want to print out a message, what you need to do, you need to
type this debug.
And then you need to use this dot log function and make first bracket and quotation.
You need to type this quotation.
And in between this quotation you can put whatever message you just want to see in console tab.
So let's suppose something like we're going to say this is start function.
So I'm going to say this is start start function.
You can just put whatever you just want to print out.
It's totally up to you.
And make sure you also close that up with semicolon.
So this debug dot log function it's going to print out the message you want to print out or want to
see.
And you make sure you just type in quotation because you need to pass a string.
And in string you have to put this quotation.
So make sure you just do that.
And let's, uh, we need to save our script.
So how we can save that?
We can simply press down Ctrl s in order to save our script or what else you can do.
You can just go over to this top left file and then click on this save over here.
And the shortcut key you can see Ctrl plus S.
So we're going to press Ctrl plus S in order to save our script.
And now let's move over to unity.
And in unity just gonna take a moment to compile our script.
Basically whatever we have done, it's gonna save.
And once it's done we are ready to go.
So we just type this function and make sure you just put at the end a semicolon.
So basically that means this line of code just gains or gets end over here.
So that is the thing.
And uh make sure you just do inside this curly brackets.
And now let's move over to unity.
And we're going to select our console tab in order to print out our want to see the message.
If you are not able to find the console tab, then you can go to window in the top.
Then scroll down to general and you can find the console tab and the shortcut key.
You can see in the right section which is Ctrl Shift plus C.
So I already have the console tab, so I'm not going to bring up that one once again.
So let's click on this play button in the top in order to play our game or in order to start our game.
And you will see in the start of the game.
So whenever the game just gonna start that message just gonna pop up.
So as you can see, as soon as the game started, we can see a message in the console says this is a
start function, why this thing gets pop up.
Because as you can see, we have done inside this start function and a start function get called in
the start of the game.
So what we're doing inside this start function we just printing out a message in the console says this
is start function as you can see.
So that's why as soon as the game started that message just gets pop up in the console says this is
star function.
So let's click on this play button in the top in order to get out of play mode.
And let's, uh, talk about the update function by now.
And we can simply just double click on this message in the console in order to move that line of code.
So we can just double click on it in order to move that line of code.
So we're going to remove this message.
And now we're going to test with this update function.
So we're going to check.
We just want to print out message this update function.
So this update function get called every single time in one second something like 60 times.
But it's going to depend what computer you're using.
If you're using superfast computer then or system then this update function in one second it's gonna
run 120 frames.
So it's gonna depend.
So let's print out a message if it's perfect, perfectly working or not.
And in order to print out message we're going to type debug dot log and make first bracket and then
quotation.
Make sure you just type this quotation and you can put whatever message.
You just want to say something like, I'm going to say this is update of date function, and let's close
that off with semicolon Ctrl S to save your script and then move over to unity.
So unity is just going to take a moment to compile our script.
And once it's done, we can just click on this play button in the top in order to play our game or in
order to start our game.
So just wait.
And now let's click on this play button in the top.
You will see that.
So just wait.
So as you can see, as soon as the game started we can see a message in the console.
This is update function just printing out over again and again.
And we can see the count over here in this console tab in the top right.
Top right you can see.
So it's already called over a hundred times, as you can see.
And the message just getting pop up every single time, because that update function runs every single
time in one second.
Uh, something like, uh, uh, you can simply say, uh, 60 frames or if you using a, a pretty fast
system or heavy system, then it's gonna run in one second, something like 120 frames.
So let's get out of play mode instead of printing out message what we want, we want this player game
object to move in the right direction.
So we need to move that.
So if we select our player game object in a hierarchy then move over to Inspector Transform and play
around with x axis.
We can see we just moving left and right.
So we just want this player game object to move in the right direction.
So let's double click on this message in order to move that line of code.
And instead of printing out message we're gonna move our player game object.
So we're gonna do inside this update function.
Why?
Because if update function runs every single frame and every single frame, we need to move this player
game object to the right direction.
So we're not going to do this start function because a start function just gonna call in the start of
the game and it's gonna call one time.
So we will stop at some point moving to the right side.
So that's why we're going to do inside this update function because we need to move our player game
object every single frame to the right direction.
So what we're going to do now we're going to type this transform.
And then we're going to use dot translate function.
So use this translate function and then make first bracket.
So this transform translate function it's going to take the direction in which direction you just want
to move.
So what we have done we just type transform basically what we're doing.
We just moving.
If we select our player game object in the hierarchy, we're just moving over to this transform component
of this player game object.
So if we move to inspector we have transform component.
So we're just moving over you.
We're using this transform.
So this transform right over here basically means this transform.
And we're using this translate function in order to move and this translate function, it's going to
take the direction in which direction you just want to move your player game object.
So we want this player game object to the move right side.
So what we're going to do we're going to move over to this line of code.
And we're going to use a vector two.
Because that vector two has x axis and y axis.
So we don't want this player game object to move in the y axis.
So we're going to put in the y axis something like zero.
And we want this player game object to move in the right side basically means x axis.
So this is x axis.
So we can select the move tool from here.
So we use we're gonna use vector two because vector two has two axis.
And we want this.
We want this player game object to move in the right side.
So we're gonna use the x axis instead of the y axis.
So we're going to do this thing.
So let's go to that line of code.
And we're going to use new.
So make sure users typing as I did over here which is new vector two and then make first bracket.
So this vector two has x axis and y axis.
So what we want we want to move this player game object horizontally.
Or you can simply say in the x axis something like this.
And we don't want this player game object to move in the y axis.
So that's why in the y axis we're going to put zero.
So 0FF basically over here referencing float numbers.
Float numbers basically those number which comes with decimal places like 1.5, 1.2, 1.9, or also
whole numbers like zero, one, two, three, four is a float number.
So make sure you just put F at the end a it's basically referencing float.
And in the x axis we want to move this player game object to the right side.
So that's why in the x axis we're going to put one f.
So let's go to script.
And we're going to put one f.
And then make sure you just close that up with semicolon.
And we're going to press down Ctrl S to save our code.
Now let's move over to unity.
So we will see this player GameObject will move to the right direction every single time one unit.
And it's gonna not move at all in the y axis.
Why?
Because in the y axis, as you can see, we put zero f and in the x one.
So every single frame this player GameObject will seem like it's just going in the right direction with
one unit every single frame.
So let's click on this play button in the top.
You will see that as soon as the game starts gonna start, you will see this player GameObject just
gonna move to the right direction.
So let's have a look on this.
So as you can see, as soon as the game started, the player GameObject just moved too much fast.
So what we have to do, let's get out of play mode and let's move over to that line of code.
So what we need, we need to multiply these things with time dot delta time.
So as you see in our player game, object moved too fast.
So that's why we need to multiply this whole thing, this vector two or in which direction we moving.
We need to multiply with time.deltatime.
Why we.
Because we need to make frame rate constant.
So let's suppose you using a a super fast computer.
So in your case the, the uh this update function over here, it's gonna run in one second 120 frames.
Uh, but I do have a little bit slow system.
So in my case, this update function in one second it's gonna run 60 frames.
So you can see there is a huge difference between our frame rate.
So that's why to make it constant, we need to multiply these things with this, uh, direction with
time.deltatime.
So we're gonna use this star symbol, sorry, this symbol in order to multiplication.
And we need to multiply with time.deltatime.
Basically, that is a frame rate of change in frame rate.
So I'm going to multiply with time dot delta time.
And make sure you just close that up with semicolon.
And let's move over to unity.
So we will see we will able to see that uh so our player game object now just going to move to the right
direction with too much slowly.
Uh let me show you.
So let's click on this play button in the top to play our game.
And let's have a look on this.
As soon as the game started, as you can see, our player game object just moving to the right direction.
But the problem is is that our speed is too much slow.
So let's get out of play mode and we're gonna stretch this ground first.
So let's select the ground GameObject in the hierarchy, and let's uh, select the right tool from here
in this scene view.
And then we're gonna move this in the Something x axis.
And I guess this is fine.
And let's go to that line of code.
So we're going to multiply this whole thing with some uh numbers, something like uh numbers.
Uh, something like five.
So our, our player game object just gonna move a little bit faster.
So let's go to that line of code and let's multiply this whole thing, uh, this whole thing, uh,
some units.
So we're gonna multiply with something like five F to make our speed a little bit fast.
So we're moving to the right direction with one unit, and we multiply it with five f.
So this will be five f in the.
So one into five f.
Just gonna be five f means this player game object will move in the right direction every single frame
with five units of speed.
So our player game object is just gonna seem it's gonna move to the right direction with a little bit
more fast.
So let's go to, uh, script and let's press down Ctrl s to save our script.
And now let's move over to unity.
So we will see this player GameObject just gonna move to the right direction with five unit of speed.
So let's have a look on this.
And let's click on this play button in the top to play our game.
And we're gonna see that.
So as soon as the game gonna start you will see this player GameObject just going to move to the right
direction with five unit of speed.
As you can see, our player GameObject is now moving with little bit fast means five F with a right
direction.
So let's get out of play mode.
But it would be nice if we able to control our speed.
So if we select our player game object then go over to Inspector in the right.
We just want to see a variable right underneath our player.
Skip basically a speed that we can define in this player game object.
So we just want to adjust over here the speed.
And we don't need to move over to this, uh, this line of code and just change this number in order
to match up a correct speed.
So I'm going to make a variable.
So we will go to top of our class.
And we're going to declare a variable variable.
You can simply say a container where we can put values.
And we can use that container or the value in order to make something.
So we're going to make it public.
So why are we making it public.
So we can see underneath our player skip right over here and the inspector so we can adjust it over
here.
So that's why we just made it public.
And the variable type just going to be float basically means decimal numbers, which comes with decimal.
And numbers like 1.2, 1.5.
Even whole numbers zero, one, two, three, four is a float number.
So the type of variable is going to be float.
And we have to name the variable or the container what we want to name it.
So I'm going to call it something like speed.
And you can just set equal to a default value.
Or you can simply close that off with semicolon as well.
But if you just want to give a default value you can set equal to and the number.
You just want to set a default value.
So I'm going to say something like five if.
And let's close that off with semicolon to end this line of code over here.
And now we're going to move over to this.
And we're going to replace this five with this speed.
So we're going to replace.
So the speed is five.
So this is going to be five.
So five into one just going to be five.
So we will move to the right direction with five unit of speed.
But we do know that we just made it public so we can adjust the number or the variable underneath our
player script in Unity in Inspector.
So we're going to save our script.
Let's press down Ctrl S to save our script.
And let's move over to unity.
So we can we're gonna be able to see that field just going to appear underneath our player script.
Why?
Because we made it public.
So that's why the variable is just going to pop up underneath our player's game.
So as you can see, as soon as our player script just gets compiled we are able to see our speed parameter.
and by default we set five.
As you can see.
So if you don't set equal to something like that and you just close that off with semicolon.
So this by default is going to be zero.
But we can adjust it over here as well.
We can set it to something like seven.
So this is just going to be seven.
So seven into one just going to be as as seven.
So we will move to the right direction with seven unit of speed.
But I'm gonna make I'm gonna make this number a little bit lower, something like uh 4.2, something
like that.
And now what we're going to do, if we click on this play button in the top, we will see this player
game object is going to move with this much speed in the right direction.
Because we set speed parameter to 4.2 as you can see.
So we're going to move uh with that much speed.
And we can just mess around with this value in order to make our player, uh, speed, uh, slow.
And we can see player is going to the right direction because we set the speed parameter to negative
value and we can set it to positive value as well.
So we can play with our while in play mode with this variable.
So this is pretty much cool thing.
And make sure you just auto play mode because it's not gonna save.
So if you just click on this play button and change something in play mode, it's not going to be safe.
So make sure you just click on this play button in the top in order to get out of play mode.
And then copy that number and you can just paste that number that you, uh, that you just could, uh,
that you, uh, as you made a good number while in play mode.
And what we do have to do now, we need to, uh, we need to move over to this inspector.
So make sure you select the player game object in the hierarchy first, then go over to Inspector,
then overrides, and then click on Apply All.
So the changes we made over here in Inspector of this player game object, it will be saved into this
player GameObject prefab as well.