Come back in this video.
We are going to take care of the camera so that it moves with our player.
So let's create a new script for that.
I'm going to create a new script and I'm going to call that one follow cam because it's the following
camera.
It follows our player, and I'm going to assign that to our main camera straightaway.
So let's drag that into its components.
Follow Cam and now let's open it up.
What we will do here is we're going to use the awake method.
We're not going to use update, but what we all need are two things.
We need the transform target.
So we need the position of the target that we need to follow.
And it's public because we want to assign it from from within unity.
So it's the target.
And then we need a private vector three, which is the offset to our player.
So it's pretty much what is our position in comparison to the player and let's set the offset within
the awake method.
So we just say offset is equal to transform the position minus the target dot position.
So we take the position of our camera itself and we subtract the position of our target, which is our
player.
So the difference here is a3d vector, which is a vector three variable here, and we save that with
an offset and now we can go ahead and update that information.
So we use the void late update method.
Late update is called every frame if the behavior is enabled.
So, so this one is used for cameras in general.
So you later update is something that you should use for camera updates, for example.
All right.
And let's go ahead transform position.
So let's change the position of our camera by the target position.
So let's set it to the target position.
But of course, we may not get the offset, otherwise our camera would be just inside of our player.
And now it's going to be based on the distance that it has from the start.
So let's go ahead and save that script.
Let's go to Unity and let's have a quick look.
So our main camera now will have a new public variable here, which we can assign target, and now we
can say our character is the target.
And as you can see now, our main camera has a position of one, five and minus five, and our character
has a position of let's set that to 00.5 and zero.
So there is a distance or a difference.
So the X difference is one, the wide difference is 4.5 and the different Z difference is minus five.
So now our main camera will always maintain that distance, that offset to our player.
Now let's save that scene and run the code.
So let's have a look if our camera follows along.
And as you can see now, I screwed that one up.
Let's do it again.
Our player is moving and great.
So our camera follows along.
Perfect.
So that's it.
Now you know how to make your camera follow your player, which is perfect for an endless runner.
In the next video, we are going to animate our player.
So see you there.