WEBVTT

00:00.080 --> 00:02.200
Let's make our camera follow.

00:02.200 --> 00:03.760
But before it, let me show you.

00:03.800 --> 00:07.600
Camera is not following our player game object at all.

00:07.600 --> 00:11.000
So let's click on this play button in the top to play our game.

00:11.000 --> 00:13.160
And let's have a look on it.

00:15.480 --> 00:19.840
You will see camera is not gonna follow our player game object at all.

00:21.120 --> 00:24.960
So player is going in the right direction as you can see.

00:25.520 --> 00:28.680
So the main cam our camera just static.

00:28.680 --> 00:32.040
It's not following at all our player game object.

00:32.040 --> 00:33.120
So let's do that.

00:33.120 --> 00:34.640
We're gonna get out of play mode.

00:35.000 --> 00:39.600
So first thing we're going to do something that we're going to create an empty game object.

00:39.600 --> 00:43.560
And then we're going to make this main camera a child of that game object.

00:43.560 --> 00:50.200
We're gonna why we're gonna do that because later on we're gonna make our shake animation of this main

00:50.200 --> 00:50.680
camera.

00:50.680 --> 00:52.240
So that's why we're gonna do it.

00:52.280 --> 00:54.960
Else our animation just gonna be weird.

00:54.960 --> 00:56.520
So that is the reason.

00:56.520 --> 01:00.280
So move over to this hierarchy, right click or hit this plus icon.

01:00.280 --> 01:03.040
Sorry I just Is clicked wrong.

01:03.040 --> 01:04.560
So I'm going to remove that.

01:05.320 --> 01:06.400
So go to hierarchy.

01:06.400 --> 01:08.480
Right click or hit this plus icon.

01:08.480 --> 01:10.240
Then create an empty game object.

01:10.240 --> 01:15.680
In the top you can see and we're going to name this game object to something like Camera Parent.

01:15.680 --> 01:20.520
So let's call it camera parent camera parent.

01:20.840 --> 01:23.840
And make sure you just go to Inspector Transform.

01:23.840 --> 01:25.960
Right click and then reset it.

01:26.120 --> 01:29.920
And what we're going to do now we're going to select our main camera in the hierarchy.

01:30.120 --> 01:32.840
Drag it top of this camera parent game object.

01:32.880 --> 01:38.280
Now as you can see the main camera is a child of this camera parent game object.

01:38.280 --> 01:43.800
So if I select Camera Parent GameObject in the hierarchy and try to move left and right, we can see

01:43.800 --> 01:46.280
the main camera is also going with it.

01:46.320 --> 01:46.640
Why?

01:46.680 --> 01:52.480
Because this main camera, as you can see in the hierarchy, is a child of this camera parent game object.

01:52.480 --> 01:53.240
So that's why.

01:53.440 --> 01:56.200
So I'm going to set the position once again to zero.

01:56.200 --> 02:00.080
So let's select the camera parent game object I'm going to set back to zero.

02:00.320 --> 02:01.320
And that's it.

02:01.720 --> 02:07.080
So what we're going to do now we're going to create a script in order to follow this player GameObject.

02:07.080 --> 02:12.200
Instead of following this player GameObject, what we're gonna do, we're going to move our camera parent

02:12.320 --> 02:16.360
or this main camera independently to the right direction.

02:16.360 --> 02:18.760
We don't want to follow our player GameObject.

02:18.760 --> 02:26.120
Instead we we're gonna make this camera parent or this main camera go to right direction with the speed

02:26.120 --> 02:28.040
of player GameObject.

02:28.040 --> 02:29.760
So we're going to make it cool.

02:29.760 --> 02:30.920
So that is the reason.

02:30.920 --> 02:38.000
So we just want this main camera or this camera parent game object to move to the right direction independently

02:38.000 --> 02:42.320
independently with the speed of our player game object.

02:42.320 --> 02:44.920
So let's create a script in order to do it.

02:44.920 --> 02:49.120
So let's head over to this project tab, then assets folder.

02:49.120 --> 02:51.080
Then head over to this scripts folder.

02:51.080 --> 02:54.640
Or you can simply right click in assets as well.

02:54.880 --> 02:57.440
So but I'm going to move over to my script folder.

02:57.440 --> 02:59.480
Let's right click there then create.

02:59.840 --> 03:02.480
And the top you can see Monobehaviour script.

03:02.480 --> 03:05.920
Or you can just scroll down a bit scripting Monobehaviour.

03:06.480 --> 03:09.120
So why are we going to create a Monobehaviour script?

03:09.120 --> 03:14.880
Because we're going to attach this script to our camera, parent GameObject or into main camera.

03:14.880 --> 03:17.680
So that's why we're going to create a Monobehaviour script.

03:17.720 --> 03:23.520
Make sure you just do that and don't create any other script, because if you create new, not going

03:23.520 --> 03:27.280
to able to attach that script and name it to something like follow.

03:27.320 --> 03:32.680
I already have created a follow script, as you can see over here in my scripts folder.

03:32.680 --> 03:37.680
So what I'm going to do now we're going to select our camera parent GameObject or the main camera in

03:37.680 --> 03:38.440
the hierarchy.

03:38.560 --> 03:39.720
And let's select the.

03:40.080 --> 03:45.880
Let's select that script from that folder and drag it over to this Add Component section in the inspector.

03:45.880 --> 03:46.320
Right.

03:46.560 --> 03:49.720
And let's open up the follow script by double clicking on it.

03:50.840 --> 03:55.080
And what we're going to do, we're going to remove this start function.

03:55.080 --> 03:57.400
And this command we don't want.

03:57.440 --> 04:02.280
So what we want we just want a reference to our player game object.

04:02.280 --> 04:08.160
So if we move over to unity we need a reference of our player game object or player skip.

04:08.240 --> 04:13.720
So if we select our player game object in the hierarchy and open up the player script by double clicking

04:13.720 --> 04:16.360
in the inspector, we can see a speed parameter.

04:16.360 --> 04:21.880
If I just go to the top of our class, we can see we have a speed parameter which is five.

04:21.920 --> 04:26.800
By default we set, but in the inspector we set which is one.

04:27.080 --> 04:34.080
So we need a reference of this player skip so we can move our camera parent game object as the speed

04:34.080 --> 04:36.560
of our player game object.

04:36.560 --> 04:39.240
So we need a reference of this player script.

04:39.240 --> 04:45.680
Then we can just simply access this speed from that player class, as you can see over here.

04:45.680 --> 04:47.680
So let's go to uh unity.

04:47.920 --> 04:49.800
And what we need to do now.

04:49.800 --> 04:51.520
Let's grab the reference first.

04:51.520 --> 04:54.960
So let's select our camera parent game object or the main camera.

04:54.960 --> 04:58.840
And let's open up our follow script by double clicking in the inspector.

04:58.840 --> 05:01.800
So we need a reference to our player Skip.

05:01.800 --> 05:09.960
So we can just move our this main camera or this camera parent game object as the speed of our player

05:10.000 --> 05:10.680
game object.

05:10.680 --> 05:16.400
So let's go to the top of our class and we're gonna grab the reference first, and we're going to make

05:16.400 --> 05:24.000
it public so we can see underneath our player's, uh, camera parent or this follow script in Inspector.

05:24.000 --> 05:27.480
So the the reference we're looking is a player Skip.

05:27.480 --> 05:33.320
So we're going to type this player, uh, or you can simply say the script that we're looking for.

05:33.320 --> 05:35.440
So you need to type it over here.

05:35.440 --> 05:39.120
So in our case player and we're going to call it we also have to name it.

05:39.120 --> 05:43.320
So we're going to call it player uh with non capitalized p letter.

05:43.320 --> 05:46.920
And let's press down Ctrl S in order to save our script.

05:46.920 --> 05:48.600
And let's move over to unity.

05:48.840 --> 05:54.120
So underneath our follow script we're going to see an air field will appear and that will be player

05:54.120 --> 05:54.600
skip.

05:54.760 --> 05:56.720
And that's just going to be empty.

05:56.720 --> 06:01.520
So what we need to do we need to drag the player script reference into that slot.

06:01.560 --> 06:04.600
So we're going to have the reference of our player skip.

06:04.600 --> 06:08.720
Then we can access the speed our player game object.

06:08.720 --> 06:15.600
So as soon as the script just gets compiled, we can see in Inspector underneath our follow script appear

06:15.600 --> 06:17.280
and it says none player.

06:17.280 --> 06:23.560
So what we need to do, we need to drag the reference of our player script here so we can just access

06:23.560 --> 06:24.240
the speed.

06:24.440 --> 06:30.480
So in our case the player script is attached to this player game object, as you can see in the inspector.

06:30.480 --> 06:33.640
So we need to drag the player game object into that slot.

06:33.680 --> 06:35.720
So let's select the camera parent game object.

06:35.720 --> 06:41.200
And we're going to select our player GameObject in the hierarchy and drag it over to this player slot.

06:41.240 --> 06:45.840
And now we can see we have a player script reference as you can see.

06:45.880 --> 06:47.480
And we are ready to move.

06:47.520 --> 06:52.560
So let's double click on this follow script in order to open up in Visual Studio.

06:52.560 --> 06:55.680
And we're going to move over to this update function Y.

06:55.680 --> 07:01.920
We're going to move over to this update function because we just want to, uh, move this main camera

07:01.960 --> 07:07.600
game object or this main camera to the right direction, uh, every single time.

07:07.600 --> 07:11.000
So that's why we're going to do inside our update function.

07:11.000 --> 07:14.510
And we do no not update function runs every single frame.

07:14.750 --> 07:22.270
So we're going to type this transform dot translate transform dot translate and make first bracket.

07:22.270 --> 07:28.830
So this transform dot translate function it's going to move our main camera or camera parent GameObject

07:28.830 --> 07:33.070
to uh to the direction in which direction we want to move.

07:33.070 --> 07:37.830
So in this bracket it's going to take the direction in which direction we want to move.

07:37.830 --> 07:40.270
So we're going to move to the right direction.

07:40.270 --> 07:44.310
So that's why we're going to type over here in brackets vector two.

07:44.790 --> 07:51.750
So we will type vector two dot write vector two dot write basically means uh one in the x and zero in

07:51.750 --> 07:52.270
the y.

07:52.310 --> 08:01.190
So we don't want to move our camera parent GameObject or our main camera to the, uh, to the Y direction.

08:01.190 --> 08:05.750
So that's why this vector two dot write basically means zero in the y.

08:05.950 --> 08:12.550
Uh, so we don't want to we're not going to move at all in the y axis, but one in the x axis to the

08:12.550 --> 08:14.030
positive direction.

08:14.030 --> 08:20.910
So every single time this are our main camera or this camera parent game object just going to move to

08:20.950 --> 08:22.270
the right direction.

08:22.750 --> 08:27.750
And uh, so basically this is the thing or you can simply type this.

08:27.750 --> 08:29.470
So a same thing.

08:29.470 --> 08:34.870
So we're going to type new vector two vector two and then make first bracket.

08:34.870 --> 08:39.350
And we do know that vector two has two axis which has x and y.

08:39.510 --> 08:42.670
So we don't want to move in the y axis at all.

08:42.710 --> 08:45.670
This camera parent game object or the main camera.

08:45.670 --> 08:48.550
So that's why in the Y we're going to put zero f.

08:48.670 --> 08:50.750
And in the x we do want to move.

08:50.750 --> 08:54.150
So that's why in the x axis we're going to put one f.

08:54.310 --> 09:01.350
So in the so our main camera or the camera parent game object will move to the right direction every

09:01.350 --> 09:03.150
single time one unit.

09:03.510 --> 09:04.870
And so that is the thing.

09:04.910 --> 09:10.350
And let's um don't and also make sure you just multiply this with Time.deltatime.

09:10.390 --> 09:13.950
Basically the this thing is same as vector two dot, right.

09:13.950 --> 09:20.710
Basically means one inch the X and 0 in 0 in the Y, and we're going to multiply with time dot delta

09:20.750 --> 09:23.470
time in order to make frame rate independence.

09:23.470 --> 09:26.630
So make sure you also multiply with time dot delta time.

09:26.790 --> 09:29.070
And let's close that up with semicolon.

09:29.070 --> 09:33.910
And we also want to multiply this one with our speed of player.

09:33.910 --> 09:36.990
And we can see we have a reference of our player skip.

09:37.030 --> 09:38.910
As you can see over here.

09:38.910 --> 09:43.750
So what we're going to do we're going to multiply this whole thing as the speed of our player.

09:43.750 --> 09:45.230
So we're going to multiply with.

09:45.270 --> 09:48.310
So multiply and we will move over to player.

09:48.470 --> 09:51.230
Then we're going to access this dot speed.

09:51.430 --> 09:54.430
And let's close that up by semicolon.

09:54.430 --> 09:58.630
And then control S in order to save your skip and move over to unity.

09:58.630 --> 10:02.590
So you will see our main camera or the camera game object.

10:02.590 --> 10:06.750
It's going to move to the right direction with the speed of our player.

10:08.190 --> 10:11.110
So firstly we just moving over to transform.

10:11.110 --> 10:17.390
And then we're using this translate function in order to move in which direction we want to move.

10:17.430 --> 10:22.910
Basically, we're just moving over to this, uh, this transform component of this camera parent game

10:22.950 --> 10:23.430
object.

10:23.790 --> 10:29.790
Uh, and then we're using translate function in order to move this camera parent or this main camera

10:29.790 --> 10:34.390
to the right side with, uh, with the speed of our player.

10:34.390 --> 10:40.550
So basically we just going over to this player script and then accessing this speed parameter.

10:40.550 --> 10:43.710
So we set, as you can see in the inspector one.

10:43.710 --> 10:49.430
So the camera parent or the main camera will move to the right direction with this much speed which

10:49.430 --> 10:50.070
is one.

10:50.390 --> 10:54.550
So let's click on this play button in the top in order to play our game.

10:54.550 --> 10:56.750
And let's have a look on this.

11:01.870 --> 11:03.070
So just wait.

11:04.950 --> 11:09.510
As you can see camera is also moving with our player game object.

11:09.750 --> 11:16.110
Uh, or you can simply say main camera just moving to the right direction as this speed of our player

11:16.150 --> 11:16.830
game object.

11:16.830 --> 11:18.510
So let's get out of play mode.

11:18.710 --> 11:22.950
And what basically we have done if we move over to this follow script.

11:22.950 --> 11:27.310
As you can see, firstly we're just going over to Camera Parent Transform.

11:27.630 --> 11:33.950
Uh, basically this camera parent transform in the inspector you can see and then we're using translate

11:33.950 --> 11:35.510
function in order to move.

11:35.550 --> 11:41.190
And this translate function, it's going to take the direction in which direction you want to move.

11:41.190 --> 11:44.670
So we're gonna which is moving right direction as you can see.

11:44.750 --> 11:48.790
And we just multiply that direction with the speed of our player.

11:48.830 --> 11:55.350
Firstly moving over to this player script and then accessing the speed parameter from that player script.

11:55.630 --> 11:59.190
And I hope you get the idea of this how you can do.

11:59.430 --> 12:05.950
And let's also make sure you just make this camera parent, uh, a prefab so you can have the copy.

12:05.950 --> 12:09.910
So we're going to select our uh camera parent GameObject in the hierarchy.

12:09.910 --> 12:16.750
And let's drag it over to this prefabs folder in our project tab over here as you can see.

12:16.790 --> 12:20.630
And now we're going to press down Ctrl S in order to save our project.
