WEBVTT

00:00.000 --> 00:00.540
Hello guys.

00:00.540 --> 00:01.620
Welcome to the video.

00:01.620 --> 00:04.680
So in this one I want to talk about basics of C sharp.

00:04.680 --> 00:11.460
And also I want to explain how Visual Studio is connected to unity and how they work together and help

00:11.460 --> 00:12.630
us to make video games.

00:12.630 --> 00:13.050
All right.

00:13.050 --> 00:17.190
So we're going to start by discussing execution order.

00:17.190 --> 00:18.720
I'm going to explain what it is.

00:18.720 --> 00:21.330
How does it work and why do we need to know about this.

00:21.330 --> 00:26.460
So in the Visual Studio over here we can see start and update.

00:26.460 --> 00:33.840
And these two are part of execution order and part of Unity's life cycle, if to be more precise, it's

00:33.840 --> 00:38.850
a part of Monobehaviours life cycle which was developed by unity.

00:38.850 --> 00:45.390
This Monobehaviour is responsible for calling start method and this one is responsible for calling update

00:45.390 --> 00:45.780
method.

00:45.780 --> 00:46.860
You can call it function.

00:46.860 --> 00:47.790
You can call it method.

00:47.790 --> 00:49.140
Both are the same.

00:49.140 --> 00:51.690
There is also a couple of other methods that you should know about.

00:51.690 --> 00:58.380
So I'm going to type them on the screen awake and there is a fixed update.

00:58.380 --> 01:00.800
Also, let me put a comment.

01:00.800 --> 01:02.150
Then there is a start.

01:02.150 --> 01:05.780
Then fixed update is called and then update after that.

01:05.780 --> 01:07.970
There is much more to that actually.

01:07.970 --> 01:10.340
And I'm going to quickly show you what I'm talking about.

01:10.340 --> 01:12.920
But don't get overwhelmed by this.

01:12.920 --> 01:13.790
Don't worry about this.

01:13.790 --> 01:16.880
It's just like for you to know the general picture.

01:16.880 --> 01:19.640
This is the entire cycle we have.

01:19.640 --> 01:24.650
You can see a week on enable, reset, start, fixed update, and so many other things.

01:24.650 --> 01:28.190
They all are important, but you don't need to remember them.

01:28.190 --> 01:29.630
You don't need to memorize this.

01:29.630 --> 01:35.570
You will learn it with practice for sure and not even all of them we're going to use during this course,

01:35.570 --> 01:37.040
so it's not that important.

01:37.040 --> 01:42.920
Let's just discuss these four because they are like most important I would say, and you should know

01:42.920 --> 01:43.520
about them.

01:43.520 --> 01:47.960
So let me briefly explain the difference between these four and how they are usually used.

01:47.960 --> 01:51.380
A wake is called once before everything.

01:51.380 --> 01:53.540
Then the start is called.

01:53.540 --> 01:56.240
Then fixed update is called and then update is called.

01:56.240 --> 01:57.560
And I'm going to say it again.

01:57.560 --> 02:02.480
Awake and start is called only once to see the difference, let's just type debug log.

02:02.810 --> 02:04.220
Awake was called.

02:06.360 --> 02:09.630
Then let's go to start method and type Debug.log.

02:09.660 --> 02:11.430
Start was called.

02:12.220 --> 02:15.250
Then in the fixed update we're going to do debug dot log.

02:15.520 --> 02:19.420
Fixed update was called and I'm going to be lazy.

02:19.420 --> 02:22.810
This time I'm going to duplicate this line of code and move it down over here.

02:22.810 --> 02:23.920
And I'm going to type.

02:26.640 --> 02:29.940
Update was called and this line of code can be removed for now.

02:29.940 --> 02:31.620
So we have four debug lines.

02:31.620 --> 02:36.060
And now if we go back to unity and play the game we're going to see them in the console.

02:36.060 --> 02:37.170
Let's go back.

02:37.320 --> 02:41.190
If you don't have this window over here let me close it.

02:41.190 --> 02:42.810
You can go to window.

02:43.530 --> 02:47.820
General console and let's just drag it over here.

02:47.820 --> 02:49.830
So over here we have a console.

02:49.830 --> 02:52.290
And now I'm going to go to play mode.

02:55.010 --> 02:58.280
And you can see awake was cold, start was cold.

02:58.280 --> 03:00.050
It was done only once, then fixed.

03:00.050 --> 03:05.870
Update is cold all the time and update is cold all the time, but much more often than fixed update.

03:05.870 --> 03:08.570
So why it's like that and why do we need it like that?

03:08.570 --> 03:16.430
Usually it's good practice to use a wake to set some relationships between scripts or getcomponent for

03:16.430 --> 03:17.840
the script that it's going to use.

03:17.840 --> 03:21.680
Basically, you know, do general setup for the game object.

03:21.680 --> 03:26.480
Then if you want to do something only once and you want to initialize something, maybe set some default

03:26.480 --> 03:30.830
values or maybe reset the score or something like that, you can do it in the start.

03:30.830 --> 03:33.800
So anything you need to do once, you can do it in the start as well.

03:33.800 --> 03:37.130
And this is what these methods are mostly used for then.

03:37.130 --> 03:43.790
Fixed update is mostly used in the games where physical calculation is very important, where physics

03:43.790 --> 03:52.370
are involved and you want everything to be even and stable because fixed update is called certain amount

03:52.370 --> 03:54.740
of times per second, if to be precise.

03:54.740 --> 03:59.000
By default we have value of 50 so called 50 times per second.

03:59.000 --> 04:00.320
That's the default value.

04:00.320 --> 04:05.510
And that is main difference from the update because update is called once per frame.

04:05.510 --> 04:13.970
And if your game is let's say 200 fps, then it will be called 240 times per second.

04:13.970 --> 04:19.610
And if your machine runs slower then it will be slower and it will be called less amount of times per

04:19.610 --> 04:20.240
second.

04:20.240 --> 04:27.710
And if you would set some physics calculations with the let's say cars movement in the update, then

04:27.710 --> 04:33.920
on one computer on the slow computer car would move slower than on the fast computer.

04:33.920 --> 04:35.450
So that is very important.

04:35.450 --> 04:42.890
And if you have some game where physics are important and you expect some FPS drops, you expect this

04:42.890 --> 04:45.020
game to run differently on different machines.

04:45.020 --> 04:49.610
Then it's better to do movement in the fixed update and other things in the update.

04:49.610 --> 04:50.600
What other things?

04:50.600 --> 04:53.780
I'm talking about such thing as input check.

04:53.780 --> 04:58.850
So what buttons player presses on the keyboard, or collision checks because you always want to be sure

04:58.850 --> 05:03.890
you have fresh information about collision between the objects, or maybe some animation updates and

05:03.890 --> 05:04.160
so on.

05:04.160 --> 05:08.420
There are many things that can be done in the update, and some of them we're going to use later on

05:08.420 --> 05:09.080
in the course.

05:09.080 --> 05:12.380
As of now, what you should know is that update is called as often as possible.

05:12.380 --> 05:16.910
So it can be used for things that you need to, uh, update as often as possible.

05:16.910 --> 05:22.370
But fixed update has a fixed time step, and it's better to be used when you need some stable reaction

05:22.370 --> 05:24.260
from anything you're trying to do.

05:24.260 --> 05:30.680
However, there is sometimes exceptions and what to use for what it is always up to you, up to the

05:30.680 --> 05:31.250
developer.

05:31.250 --> 05:34.250
It always depends on the project and on the developer.

05:34.250 --> 05:40.730
For example, in this video game, we're going to uh, we're going to update movement in the update

05:40.730 --> 05:42.260
method, not fixed update.

05:42.260 --> 05:47.390
And again I just say that if you want to update movement evenly and same on all of the machines, it's

05:47.390 --> 05:48.800
better to do it in the fixed update.

05:48.800 --> 05:51.830
But in our case we're going to have a very simple game.

05:51.830 --> 05:57.680
I really doubt that this game will lag on any computer or any mobile phone.

05:57.680 --> 06:02.480
In fact, this is the second time I making this project and I know it will not lag on other machines,

06:02.480 --> 06:03.650
so it's going to be fine.

06:03.650 --> 06:09.080
It will have high frame rate, so if we don't expect the FPS to drop, we can do it in the update.

06:09.080 --> 06:11.810
Apart from that, this is a platformer game.

06:11.810 --> 06:17.060
In the platformer games, it is very important to have responsive controls and it's very important to

06:17.060 --> 06:18.650
have everything fast.

06:18.650 --> 06:20.570
And you know again responsive.

06:20.570 --> 06:24.980
So because of that we're going to do everything in the update because of that and also for educational

06:24.980 --> 06:25.460
purpose.

06:25.460 --> 06:31.520
And if in some cases we'll have problems with the performance we can always fix it.

06:31.520 --> 06:34.070
But I'm telling you it will not happen for sure.

06:34.070 --> 06:35.480
So we're not going to use fixed update.

06:35.480 --> 06:37.280
We're going to use only update.

06:37.280 --> 06:41.450
Also, for those who are familiar with unity a little bit, I just want to say that if you're using

06:41.450 --> 06:48.410
Rb's velocity in the update, unity will take care of it and it will make sure the speed of the character

06:48.410 --> 06:51.470
is consistent, even if your FPS is higher than before.

06:51.470 --> 06:53.510
So we don't have to worry about anything here.

06:53.510 --> 06:54.470
Okay, great.

06:54.470 --> 06:57.770
Now that we got that out of the way, now let's discuss something else.

06:57.770 --> 06:59.120
I'm going to remove this.

07:00.060 --> 07:03.930
And for now I will remove starting awake as well.

07:03.960 --> 07:05.340
Actually we can remove update as well.

07:05.370 --> 07:07.260
We're not going to use it now, so let's remove it.

07:07.260 --> 07:14.160
And let's talk about variables and let's talk about how do we make them, how we declare them, how

07:14.160 --> 07:16.620
we assign the variable and why do we need them at all.

07:16.620 --> 07:19.710
So as of now, if we go to inspector it's going to be empty.

07:19.710 --> 07:21.420
There is nothing there on the player.

07:21.420 --> 07:21.960
Right.

07:22.050 --> 07:22.950
Wait a second.

07:23.800 --> 07:25.360
Yeah, except the rigid body.

07:25.360 --> 07:26.680
And it's feasible.

07:26.680 --> 07:31.420
And I suppose we want to have a speed for the player so we know how fast he can run.

07:31.420 --> 07:34.150
For that we would have to make a variable.

07:34.150 --> 07:38.050
When you make a variable you need to define is it going to be public or private.

07:38.050 --> 07:43.120
And I'm going to talk about public and private in a minute in the end of the video actually as of now,

07:43.120 --> 07:45.190
just try to type after me okay.

07:45.190 --> 07:47.800
Let's type public load.

07:48.370 --> 07:49.150
Speed.

07:50.220 --> 07:51.300
That's all what we need.

07:51.300 --> 07:52.230
And let's save it.

07:52.230 --> 07:58.590
Now you can see in the inspector we've got new field that says speed, and there is a number.

07:58.590 --> 08:04.830
And if you drag it like so or type something in here you can see you make some number here.

08:04.830 --> 08:07.500
And this can be used to define speed of the character.

08:07.500 --> 08:11.640
It is like that because we declared a variable of a type float.

08:11.640 --> 08:14.640
This is one of the data types that we use in in video games.

08:14.640 --> 08:16.590
And it has name speed.

08:16.590 --> 08:21.420
Basically you define what type of data you want to have in the can.

08:21.420 --> 08:23.430
And let's say this one going to be float.

08:23.640 --> 08:27.690
And then you can have lots of variables like that.

08:27.690 --> 08:29.820
You can have tens or hundreds of floats.

08:29.820 --> 08:31.830
So you need to know which one is which.

08:31.830 --> 08:33.240
And for that we give it a name.

08:33.240 --> 08:35.820
And for example we assign this as speed.

08:35.820 --> 08:40.290
And it's just like that you make a variable of a certain type, then you give it a name.

08:40.290 --> 08:48.570
In our case we have type float which is responsible for storing numbers with decimals such as 3.5,

08:48.570 --> 08:49.830
1.2 and so on.

08:49.830 --> 08:51.150
And it has a name speed.

08:51.150 --> 08:52.680
This is how you declare a variable.

08:52.680 --> 08:53.880
There is another data types.

08:53.880 --> 08:54.870
There are lots of them.

08:54.870 --> 09:00.900
But in this video I'm just going to tell you about main three data types that we're using public int

09:00.900 --> 09:03.870
and let's call it as coins for example.

09:03.870 --> 09:07.440
And this one is numbers without decimals.

09:07.440 --> 09:10.470
This one is responsible for having the whole number.

09:10.470 --> 09:11.190
Example.

09:11.190 --> 09:14.940
Let's go here and just try to change the value and see what we have.

09:15.650 --> 09:19.280
It can only be a number without dot, right?

09:19.280 --> 09:25.790
And this is useful when you want to have maybe coins enemies amount anything like that because you can't

09:25.790 --> 09:27.320
have 4.5 coins, right?

09:27.320 --> 09:29.120
You can have only 4 or 5.

09:29.120 --> 09:29.930
So this is the case.

09:29.930 --> 09:30.890
It can be used for.

09:30.890 --> 09:33.620
But for the speed it can be changed to any value.

09:33.620 --> 09:34.580
Even with decimal.

09:34.580 --> 09:36.200
There is another data type.

09:36.560 --> 09:38.720
Let's do public bool.

09:39.600 --> 09:45.510
And I'm going to name it as is that for example, can be true or false, and only that this is very

09:45.510 --> 09:48.150
useful for controlling state of your character.

09:48.150 --> 09:50.130
Like is he knocked away?

09:50.130 --> 09:52.800
Maybe he's dead, maybe he's running, maybe he's falling.

09:52.800 --> 09:57.480
All of that can be used with the help of booleans, and it can be only true or false.

09:57.480 --> 09:59.160
True or false, nothing else.

09:59.160 --> 10:05.250
There is another simple data type that is like out there, but it's not used much in development, especially

10:05.250 --> 10:06.090
in the beginning.

10:06.240 --> 10:08.910
It's a public stream player name, let's say.

10:08.910 --> 10:11.160
And this one is text.

10:11.160 --> 10:12.150
Simple as that.

10:12.150 --> 10:14.730
If we save this and go back to unity.

10:17.170 --> 10:20.020
Here you can just type Alex for example.

10:20.020 --> 10:20.260
Right.

10:20.260 --> 10:21.760
So this is for text.

10:21.760 --> 10:24.070
And these are for simple data types.

10:24.070 --> 10:25.660
And there are much more of them.

10:25.660 --> 10:27.040
And we're going to use lots of them.

10:27.040 --> 10:29.140
But don't worry about it.

10:29.140 --> 10:30.670
Don't try to memorize everything.

10:30.670 --> 10:32.710
You just need to know that there are different types.

10:32.710 --> 10:34.510
And what is the structure here.

10:34.510 --> 10:36.610
Like why do we type public?

10:36.610 --> 10:37.690
Why do we type float?

10:37.690 --> 10:38.710
Why do we type speed?

10:38.710 --> 10:41.140
Because we decide if it's a public or private.

10:41.140 --> 10:43.630
Then we decide what type of data we want to have.

10:43.630 --> 10:44.800
Then we give it a name.

10:44.800 --> 10:45.580
Simple as that.

10:45.580 --> 10:47.290
And there again, there are lots of them.

10:47.290 --> 10:54.430
There is public image, there is color, there is a transform, there is a animator.

10:54.430 --> 10:55.510
There are so many of them.

10:55.510 --> 10:57.280
And we're going to use most of them eventually.

10:57.280 --> 11:01.660
So as of now, just don't worry about it and try to remember the structure.

11:02.260 --> 11:03.880
The access type.

11:04.240 --> 11:06.160
The data type and the name.

11:06.160 --> 11:08.410
Now we have four public variables.

11:08.410 --> 11:10.000
Let me delete two of them.

11:11.070 --> 11:13.440
And this one is private.

11:13.800 --> 11:14.880
I'm going to save it.

11:14.880 --> 11:17.190
And let's go to Inspector.

11:18.940 --> 11:20.710
And try to see the difference.

11:20.950 --> 11:23.740
What is the difference between speed and coins right now?

11:23.860 --> 11:26.290
Except that this is float and this is integer.

11:27.090 --> 11:28.320
This is public.

11:28.320 --> 11:30.060
And it can be.

11:31.000 --> 11:33.250
Seen in the inspector over here.

11:33.250 --> 11:36.730
And this is private, and it cannot be seen in the inspector.

11:36.730 --> 11:37.570
It is not there.

11:37.570 --> 11:38.650
We cannot see it.

11:39.380 --> 11:46.430
This is one of the differences, and also public variable can be accessed from other script in the project.

11:46.430 --> 11:51.170
Like maybe you have some, you know, player manager that can control speed of the character.

11:51.170 --> 11:53.720
If that's the case, you need to make variable public.

11:53.720 --> 11:59.450
But in general, it's good practice to make variables public only if you have a good reason to do so.

11:59.450 --> 12:03.860
Other than that, it's better to keep variable private like we have here with the coins.

12:03.860 --> 12:09.830
When something is private, it means it is available only to the script it was created in.

12:09.830 --> 12:14.600
So if we make private variables inside of a player, they will be available only for player.

12:14.600 --> 12:19.070
If we make public variable, it will be available to any other script in the future.

12:19.070 --> 12:19.520
Okay.

12:19.520 --> 12:22.970
And now I said that we need to keep variables private.

12:23.300 --> 12:23.870
Correct?

12:23.870 --> 12:24.380
Correct.

12:24.380 --> 12:28.220
But we made Rigidbody public here because we need to assign it.

12:28.220 --> 12:28.670
Right.

12:28.670 --> 12:32.900
If we hide it and make it private, it will not be visible in the inspector.

12:33.800 --> 12:39.080
And if it's not visible in the inspector, we cannot drag rigidbody here and assign it right.

12:39.080 --> 12:40.550
That's the problem.

12:40.550 --> 12:43.820
So for that case, we can open a wake.

12:45.400 --> 12:50.800
And in their wake we can type RB equals to get component of rigid body 2D.

12:51.670 --> 12:56.710
And this one will search for all of the components on this game object.

12:56.710 --> 13:01.450
If the script is on this game object, it will search components on this game object and it will check

13:01.450 --> 13:05.890
if there is a rigid body and if there is a rigid body, it will assign it to the RGB value.

13:05.890 --> 13:07.780
So now we don't have to assign it manually.

13:07.780 --> 13:10.750
We can just do it in the script and it will be assigned.

13:10.750 --> 13:12.550
We can go to play mode now.

13:13.690 --> 13:16.840
And then we can go here and click debug.

13:16.840 --> 13:22.210
And you will see that in the player over here we have rigidbody assigned same as before.

13:22.630 --> 13:23.620
Very cool right.

13:23.620 --> 13:24.730
Very simple.

13:25.590 --> 13:27.600
Now let's go back and do something else here.

13:27.600 --> 13:29.070
Just a bit more of a clean up.

13:29.070 --> 13:31.620
I'm going to delete these coins because we're not going to need them.

13:31.980 --> 13:34.080
And let's discuss this speed value.

13:34.080 --> 13:38.940
So we have to keep it private as well because we don't have a good reason to keep it public.

13:39.420 --> 13:40.770
Let's make it private.

13:42.730 --> 13:45.070
And now it is not visible in the inspector.

13:47.270 --> 13:49.460
However, we still want to change the value.

13:49.490 --> 13:53.150
We still want to be able to test different speed value on the player.

13:53.570 --> 13:58.910
For that, we can go back to script and type here square brackets, serialized field.

13:59.390 --> 14:05.600
And this will make it visible in the inspector and it will keep it private at the same time.

14:05.900 --> 14:07.160
Let's save this.

14:09.330 --> 14:12.000
And here we have speed that can be changed as well.

14:12.150 --> 14:17.220
Now, you might ask me why not make rigid body serialized field and assign it in the inspector?

14:17.430 --> 14:18.900
Yes, it can be done.

14:18.900 --> 14:20.430
That would not be wrong.

14:20.430 --> 14:25.830
But personally me, I prefer to have a less information in the inspector as possible.

14:25.830 --> 14:31.380
If we can just assign IRB once and not to worry about it later on, it is better to hide it and assign

14:31.380 --> 14:36.120
it in the script, because later on this player script will have lots of variables.

14:36.120 --> 14:38.880
It's going to be maybe like.

14:39.710 --> 14:41.120
That, you know.

14:41.120 --> 14:43.610
So we need to hide as much information as we can.

14:43.610 --> 14:45.890
Well, that's it for this video.

14:45.890 --> 14:48.530
I know it was lots of information.

14:48.530 --> 14:52.790
I understand you very well, but we don't need to know actually, anything else for now.

14:52.790 --> 14:54.890
And we can start development of the character.

14:54.890 --> 14:57.200
And we're going to do that in the next video.

14:57.200 --> 14:58.880
Okay I'll see you there.

14:59.240 --> 14:59.660
Bye bye.
