Let's make our camera follow.
But before it, let me show you.
Camera is not following our player game object at all.
So let's click on this play button in the top to play our game.
And let's have a look on it.
You will see camera is not gonna follow our player game object at all.
So player is going in the right direction as you can see.
So the main cam our camera just static.
It's not following at all our player game object.
So let's do that.
We're gonna get out of play mode.
So first thing we're going to do something that we're going to create an empty game object.
And then we're going to make this main camera a child of that game object.
We're gonna why we're gonna do that because later on we're gonna make our shake animation of this main
camera.
So that's why we're gonna do it.
Else our animation just gonna be weird.
So that is the reason.
So move over to this hierarchy, right click or hit this plus icon.
Sorry I just Is clicked wrong.
So I'm going to remove that.
So go to hierarchy.
Right click or hit this plus icon.
Then create an empty game object.
In the top you can see and we're going to name this game object to something like Camera Parent.
So let's call it camera parent camera parent.
And make sure you just go to Inspector Transform.
Right click and then reset it.
And what we're going to do now we're going to select our main camera in the hierarchy.
Drag it top of this camera parent game object.
Now as you can see the main camera is a child of this camera parent game object.
So if I select Camera Parent GameObject in the hierarchy and try to move left and right, we can see
the main camera is also going with it.
Why?
Because this main camera, as you can see in the hierarchy, is a child of this camera parent game object.
So that's why.
So I'm going to set the position once again to zero.
So let's select the camera parent game object I'm going to set back to zero.
And that's it.
So what we're going to do now we're going to create a script in order to follow this player GameObject.
Instead of following this player GameObject, what we're gonna do, we're going to move our camera parent
or this main camera independently to the right direction.
We don't want to follow our player GameObject.
Instead we we're gonna make this camera parent or this main camera go to right direction with the speed
of player GameObject.
So we're going to make it cool.
So that is the reason.
So we just want this main camera or this camera parent game object to move to the right direction independently
independently with the speed of our player game object.
So let's create a script in order to do it.
So let's head over to this project tab, then assets folder.
Then head over to this scripts folder.
Or you can simply right click in assets as well.
So but I'm going to move over to my script folder.
Let's right click there then create.
And the top you can see Monobehaviour script.
Or you can just scroll down a bit scripting Monobehaviour.
So why are we going to create a Monobehaviour script?
Because we're going to attach this script to our camera, parent GameObject or into main camera.
So that's why we're going to create a Monobehaviour script.
Make sure you just do that and don't create any other script, because if you create new, not going
to able to attach that script and name it to something like follow.
I already have created a follow script, as you can see over here in my scripts folder.
So what I'm going to do now we're going to select our camera parent GameObject or the main camera in
the hierarchy.
And let's select the.
Let's select that script from that folder and drag it over to this Add Component section in the inspector.
Right.
And let's open up the follow script by double clicking on it.
And what we're going to do, we're going to remove this start function.
And this command we don't want.
So what we want we just want a reference to our player game object.
So if we move over to unity we need a reference of our player game object or player skip.
So if we select our player game object in the hierarchy and open up the player script by double clicking
in the inspector, we can see a speed parameter.
If I just go to the top of our class, we can see we have a speed parameter which is five.
By default we set, but in the inspector we set which is one.
So we need a reference of this player skip so we can move our camera parent game object as the speed
of our player game object.
So we need a reference of this player script.
Then we can just simply access this speed from that player class, as you can see over here.
So let's go to uh unity.
And what we need to do now.
Let's grab the reference first.
So let's select our camera parent game object or the main camera.
And let's open up our follow script by double clicking in the inspector.
So we need a reference to our player Skip.
So we can just move our this main camera or this camera parent game object as the speed of our player
game object.
So let's go to the top of our class and we're gonna grab the reference first, and we're going to make
it public so we can see underneath our player's, uh, camera parent or this follow script in Inspector.
So the the reference we're looking is a player Skip.
So we're going to type this player, uh, or you can simply say the script that we're looking for.
So you need to type it over here.
So in our case player and we're going to call it we also have to name it.
So we're going to call it player uh with non capitalized p letter.
And let's press down Ctrl S in order to save our script.
And let's move over to unity.
So underneath our follow script we're going to see an air field will appear and that will be player
skip.
And that's just going to be empty.
So what we need to do we need to drag the player script reference into that slot.
So we're going to have the reference of our player skip.
Then we can access the speed our player game object.
So as soon as the script just gets compiled, we can see in Inspector underneath our follow script appear
and it says none player.
So what we need to do, we need to drag the reference of our player script here so we can just access
the speed.
So in our case the player script is attached to this player game object, as you can see in the inspector.
So we need to drag the player game object into that slot.
So let's select the camera parent game object.
And we're going to select our player GameObject in the hierarchy and drag it over to this player slot.
And now we can see we have a player script reference as you can see.
And we are ready to move.
So let's double click on this follow script in order to open up in Visual Studio.
And we're going to move over to this update function Y.
We're going to move over to this update function because we just want to, uh, move this main camera
game object or this main camera to the right direction, uh, every single time.
So that's why we're going to do inside our update function.
And we do no not update function runs every single frame.
So we're going to type this transform dot translate transform dot translate and make first bracket.
So this transform dot translate function it's going to move our main camera or camera parent GameObject
to uh to the direction in which direction we want to move.
So in this bracket it's going to take the direction in which direction we want to move.
So we're going to move to the right direction.
So that's why we're going to type over here in brackets vector two.
So we will type vector two dot write vector two dot write basically means uh one in the x and zero in
the y.
So we don't want to move our camera parent GameObject or our main camera to the, uh, to the Y direction.
So that's why this vector two dot write basically means zero in the y.
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
positive direction.
So every single time this are our main camera or this camera parent game object just going to move to
the right direction.
And uh, so basically this is the thing or you can simply type this.
So a same thing.
So we're going to type new vector two vector two and then make first bracket.
And we do know that vector two has two axis which has x and y.
So we don't want to move in the y axis at all.
This camera parent game object or the main camera.
So that's why in the Y we're going to put zero f.
And in the x we do want to move.
So that's why in the x axis we're going to put one f.
So in the so our main camera or the camera parent game object will move to the right direction every
single time one unit.
And so that is the thing.
And let's um don't and also make sure you just multiply this with Time.deltatime.
Basically the this thing is same as vector two dot, right.
Basically means one inch the X and 0 in 0 in the Y, and we're going to multiply with time dot delta
time in order to make frame rate independence.
So make sure you also multiply with time dot delta time.
And let's close that up with semicolon.
And we also want to multiply this one with our speed of player.
And we can see we have a reference of our player skip.
As you can see over here.
So what we're going to do we're going to multiply this whole thing as the speed of our player.
So we're going to multiply with.
So multiply and we will move over to player.
Then we're going to access this dot speed.
And let's close that up by semicolon.
And then control S in order to save your skip and move over to unity.
So you will see our main camera or the camera game object.
It's going to move to the right direction with the speed of our player.
So firstly we just moving over to transform.
And then we're using this translate function in order to move in which direction we want to move.
Basically, we're just moving over to this, uh, this transform component of this camera parent game
object.
Uh, and then we're using translate function in order to move this camera parent or this main camera
to the right side with, uh, with the speed of our player.
So basically we just going over to this player script and then accessing this speed parameter.
So we set, as you can see in the inspector one.
So the camera parent or the main camera will move to the right direction with this much speed which
is one.
So let's click on this play button in the top in order to play our game.
And let's have a look on this.
So just wait.
As you can see camera is also moving with our player game object.
Uh, or you can simply say main camera just moving to the right direction as this speed of our player
game object.
So let's get out of play mode.
And what basically we have done if we move over to this follow script.
As you can see, firstly we're just going over to Camera Parent Transform.
Uh, basically this camera parent transform in the inspector you can see and then we're using translate
function in order to move.
And this translate function, it's going to take the direction in which direction you want to move.
So we're gonna which is moving right direction as you can see.
And we just multiply that direction with the speed of our player.
Firstly moving over to this player script and then accessing the speed parameter from that player script.
And I hope you get the idea of this how you can do.
And let's also make sure you just make this camera parent, uh, a prefab so you can have the copy.
So we're going to select our uh camera parent GameObject in the hierarchy.
And let's drag it over to this prefabs folder in our project tab over here as you can see.
And now we're going to press down Ctrl S in order to save our project.