Welcome back, My favorite game developers.
In this video, we are going to be creating
our first script and adding it to our doom player.
Not only that, we are going to be learning a lot of things,
we are going to be learning what this scary word means.
We're going to learn about variables.
We're going to learn about inputs.
We're going to learn about vector 2.
We are going to be learning how start and update work separately.
So with that said,
this is a bit of a longer video if you want,
but if you're a beginner, it's very important,
very crucial to understand everything.
So let's not waste any more time and let's get started.
Oh, okay, so time for some scripting.
The first thing we're going to do is we're going to create
a script and assign it to the doom player.
Now, there are many ways of creating scripts and there
are many ways of assigning scripts to certain objects.
They are all the same.
Let's start off with the most basic one,
and that is simply clicking on the game object.
You can see right here that we have the transform.
It is one of the components that we have.
We can add another component right here by clicking on it.
Now, you can see that we have many components that we can add,
ranging from audio to affect,
to physics and so on.
But we also have this option right here of adding scripts.
We can click on it. We can see through it and add whichever script that we want.
Or even easier than that,
we can simply start typing in the name of the script that we want to add.
And the script that we want to call or create will be the player controller.
So we're going to create layer controller right here and make
sure that you create the name to be a single sentence without any spaces.
You can see right here we have the option for a new script.
Let's click on it.
You can see the name as player controller.
And now we can create the script,
but make sure that shoe figure out correct word or the correct
name for the script because it will be the name of the class inside of this script.
This will be very crucial as you create scripts later on.
So we're going to create an ad.
Wait for just a little bit.
And now we should see that we have the script ready.
If we click on it right here,
we can see in the Inspector what this script will look like.
And now we are going to double-click.
And you can see that Visual Studio is opening up.
Now.
One of the things many people or many students of mine keep asking me over and over.
If your script doesn't open up using Visual Studio, or for example,
when you type something in here and you don't get the recommendations by Visual Studio,
you can go into the that, go to Preferences,
go to the External Tools,
and in the external script editor,
make sure that you have Visual Studio Community.
You can choose our IDEs like Visual Studio Code.
But me personally, I like or prefer to use the Visual Studio Community 2019.
It's much better and smoother than the studio.
Sometimes studio just has its quirks where it doesn't show up,
doesn't open up correctly.
So I prefer using Visual Studio Community.
That's free.
It works.
Let's continue on.
Now.
We have our script opened up right here.
We have the libraries that we can use.
We have the class name, the player controller.
It should always have the same name as the script that we've created right here.
So you can see player controller under no circumstances do you
change the name of the script in here without changing the class name?
Don't even try to do that.
It will create a lot of problems.
What do we have here? It's a public,
we'll see what that means.
It's a class, it's mono behavior.
Monte behavior gives us the ability to use things such as start and update.
What is start? Start is a method.
We'll see later on what methods are.
But for now all you need to know any code that we write inside of
the star gets executed as soon as we start our game,
if that object is turned on,
of course, we'll see what that also means later on.
An update, as you can see,
update is called once per frame.
So if we have, for example,
things that we want to call every single frame update is perfect
for that and we will see all of this as we progress through this course.
You'll understand everything, don't worry about it.
Now, the first thing we need to do is we need a way to move our player.
We will do that by taking inputs from the arrow keys or the WASD keys.
But before we do that,
there is something that we need to take into consideration and that
is the movement speed of our player.
And for that, we are going to need a variable.
What is a variable when it comes to C-sharp? Let me explain.
Variables are like boxes.
So we have a box,
we give it the name lives,
and we put inside that box a number three.
So now we have a variable obtain lives that has a three inside of it.
So this is how we create the code for this box or valuable.
We start off by giving the accessibility.
So where can you access this variable?
Can either be public or it can be private.
If it's private, we can only access it inside
of the class we've created, not from outside.
If it's public, we can exit from any other script that we want.
We need to assign the type of the variable.
So we have multiple types,
we have integers, strings and so on.
We will see in just a bit what other types we have.
We have the name of the variable.
Obviously, every variable should have a name.
How else do we call it?
And we have the data that is inside of this variable.
So this is how we create a variable in C Sharp.
And this is the representation if you want,
It's just like a box that we put things and then we move on.
We have different types of variables.
And the first type is a velocity.
So an integer is a normal number, let's say 123456.
A float is, let's say 4.8.
Why do we have a float?
Because sometimes the speed of something or the velocity of a car isn't just five or six.
It can be 6.32,
it can be 6.9, it can be 6.9456.
So we need a float,
and this is how we create a float.
We always add the data.
So the 4.8 with an F on the end.
Then we have a Boolean.
The Boolean is usually either true or it's false.
So for example, let's say we want to determine whether a player is dead or not.
So as he, that he is dead,
then the variable is true.
If he's not dead,
then the variable is that S false.
Finally, we have a string.
So for example, what is a string of characters?
We have multiple characters,
one after the other.
I see h, e, l.
That string of characters is Michael.
So we call a variable of type string with the name my name,
and it represents michael.
Make sure whenever you create a string variable that is between these two quotes.
Okay, now, with all these variables done with,
Let's go ahead and create our own variable in the scripts right here.
So the first thing we're going to do,
as we are going to create something called a serialized field.
Now, don't be scared of this long name.
It's going to be an integer,
and it's going to be called the movement speed.
Now this will determined how fast our player moves as we
implemented your understand and why are we using something called the serialized field.
You just told me, Michael,
that it's either public or private.
What does this serialized field Meet?
Well, don't worry about it.
Later on.
I will also explain why we are using something called a serialized field.
But for now understand that it is something that we can see in the Inspector.
So if I make this public,
save this, go back into our script.
You'll see that in the inspector this changes.
We have the public and movement.
And if we go to the doom player,
you can see right here that we have the movement speed and we can change it.
But if I make it private so that other scripts don't access it,
you'll notice right here that we no longer can change it.
But what if we want to be able to change it from the Inspector,
but at the same time,
we don't want other scripts to access it.
Well, in that case,
we create a serialized field.
We save that.
We go back and do inspector.
And you can see right now,
we can't change it,
but other scripts cannot access it.
We'll look into that later on as we progress when we have other scripts.
Great, so now we have the variable of the movement speed.
The next thing we want to get is d,
movement, direction or the input that we get.
So how are we going to move our player?
Well, we are going to need two inputs.
One which will move the player left and right,
or in the horizontal direction,
and one which will move the player up and down,
which is the vertical direction.
So the way we do that is by using the input system and we're going to
get our inputs using something called the input dot get access Raw.
Now, you might be confused. What is that?
How are we going to get this?
Well, whenever you face some maybe.
Maybe a new method or
some weird thing that we are using in unity and you're a bit confused.
You have a very helpful friend, which is Google.
And then when you look it up in Google,
you can see that we have something called the Unity documentation,
which is such an amazing place and it should be a daily source of information for you.
So we want to use input.
We want to get the axes,
which are the horizontal and vertical.
You can see right here,
if we look for it and the documentations,
we have all the information that we need.
First of all, we can see how the declaration works.
We can see the description so it returns
the value of the virtual access identified by axis name.
It also tells you how to use these different methods.
And also it gives you right here a example of how to use it.
And lo and behold,
they use it and the update,
we can even go ahead and copy this and use it in our own king.
But for now, we're just going to look up the input dot,
get access row, horizontal.
Very nice. But where did they get the horizontal name from?
Let's check it out.
If we go back and our game.
If we go to the file or let me see the Edit Project Settings.
If we look through the input manager,
we click on the axis,
we can see that we have the horizontal,
vertical firewall to jump,
mouseX fire jump, et cetera.
These are all the inputs that we can use.
And if we click on the Horizontal,
you can see right here that we have the name horizontal,
hence why it's being used right here.
Whereas here you can see that we are using horizontal.
And you also have the vertical and you can see the negative button is left,
the positive button is right.
And you also have an out negative button, which means alternative,
which is the a and the D. You also have the gravity and so on,
but of details in there.
And you also have the axis,
the x axis, which means the horizontal.
So let's go ahead in our code and implement this.
So we're going to open up the cold right here.
They used it an update.
Let's go ahead and use it and update because we want to
get access to it every single time.
So we're going to write in here input dot, get access wrong.
And we are going to open up the two brackets in here.
But now we want to write horizontal.
Let's say we write hurry xanthan.
So you can see, I kind of messed it up.
It's not the correct word.
If it's not the exact same word as the one used right here in the Project Settings.
This will not work.
What does that mean?
Whenever we are using the string references,
we already learned what strings are.
We need to make sure that we go into Unity.
Double-click in here to select it,
Control C to copy it,
go back here and paste.
This was assure that we'd not make any mistake whenever
we are creating or using a string reference.
This is very important because when it comes to errors,
this will not be detected by the Visual Studio.
That's why it's imperative whenever you are using a string reference to always,
always copy it and paste.
This is a rule that you will use all your journey game development.
Now that we have the axis roll on horizontal.
We can also add the vertical,
but it's not enough.
We need a way to store what we are clicking.
So the way we do that is we'll create another variable in here.
This time it's going to be private.
It's going to be of type vector 2 and it's going to be the movement.
And now what is a vector two and why we use it in order to have the movement input.
Well, because if we look back in here and wait for it to compile,
you can see that the position is an x, y, and z.
Now, because we are not working in VC dimension and this dimension,
whatever you wanna call it,
we are only working in the x and y.
We want to have a vector to that will take in both the x and the y.
So now what we can do in our code,
we can come here.
We can write movement input.
We can add the dot x and it's going to be equal to the input dot.
Get access wrong.
And just to make sure that we can see everything,
we are going to write debug dot log.
And in here we are just going to print out the movement,
that foot dot x.
We will save that.
Let's also go ahead and print out the debug dot log and the whole movement input.
So I'm going to write in here,
this is the vector.
To save that, go back into Unity and make sure you have the console right here.
If you don't go to Window, go to General.
And you can see right here you have the console window.
So we're going to click on here.
Don't worry about these small errors.
Run the game.
There we go. So we can see that we currently have zeros.
We can click on the collapse.
This will collapse all the messages that are the same.
If we now try to press a or D,
if we press on the a, there you go.
You can see that this becomes minus 1 and vector 2 is minus 1 000.
If we press the d key,
you'll notice that it becomes 1 and the vector two is 110.
So there we go.
Excellent. Now that you understand how these work, how these look,
it's time for a small challenge and your challenge is going to be to get the y input.
So check in Project Settings Input Manager for the y input, what should it be?
What should the string reference me?
Get the input and code using the gap axis roll.
You can also check out something called the get access without row.
This is a good The extra resource for you to do.
It's very easy, just checking the documentation,
the just get access without Rob, see what's difference.
And finally, you will need to display that input on the console using the debug dot log,
just like we did using the movement input.
So pause the video right now and go do the each gallon.
Okay, welcome back.
How did you get on with that?
I always hope that you are trying your best.
So we are going to duplicate this line.
How would we do that? Simply click on the end of this line.
Hold control D.
Now you have the exact same line.
We're going to change this from the x to the y.
There you go.
And then the input.txt get access.
Rob, what should this be?
Well, we know this should be vertical,
but whenever we are dealing with string references,
we go to the vertical,
double-click Control C, go back in the code control V, save that.
And let's duplicate this also,
movement input dot y.
And let's just add a bit of words in here.
So this will be the x,
this will be the y.
Save all of that,
go back into unity.
That's close the project settings window.
Run the game. So now as you can see both the x,
the y, and the vector or all 0.
And you can see how many we have in here.
Once we press the w,
We can see that we have the y as one,
the vector 2, 0, 1.
If we press the S, it's minus1.
If we press the a,
x is minus1 while the y is still 0,
you can see it right here in the vector 2.
If we press both the W and the D key,
you can see that we have this vector is 1,
1 with x being one.
And everything is working perfectly fine with everything.
So I hope you enjoy it.
I hope you understood what's happening.
If you're not sure what's happening.
Tried to look through this again,
understand what a vector two means.
We also have something called the vector 3.
Whenever we are working with VC dimensions,
if it's a 3D game.
And with that said,
I hope you enjoy and hope you also understood how update is being
called every single frame you can see how many times it's being called.
And just for a small demonstration,
if you're still a bit skeptical of what I'm saying,
Let's try debug dot log.
And let's just say in here, hey,
I'm and start being called once.
Saved that back in our game when we run,
as soon as this assembles, run the game.
You'll see right here, hey,
is called only once.
And these are being called every single frame.
And our game with that said,
back in here, commit all of our changes.
Commit.
And what should we call it in here?
Created a script for the player and learned variables.
Now, this is because we are creating a course usually when you're
doing a project and learned variables, isn't that important?
I'm going to remove it.
That's just say created a script for the layer.
We are going to commit our changes.
And I'll see you in the next video.