Hello, guys.
Welcome to the video.
Uh, in this section, we're going to work on camera.
I just want to explain how camera works, how you can use the camera, how you can set up dynamic camera.
Also, I want to show you a couple of tricks to improve game feel.
And in general, I think it's a good knowledge to know how to work with camera.
We're going to use Cinemachine in this project.
We need to go to Window Package Manager and in unity registry we need to find Cinemachine.
There it is.
So I'm going to just install it.
It will take a couple of seconds, but not that long.
Now we can go to one of the scenes, one of the levels, let's say level one, and we're going to set
up camera here.
I'm going to explain how it's going to work.
So now let's add the camera.
We're going to click create button Cinemachine targeted cameras to the camera.
Very nice.
So now we've got the camera.
And before I explain any settings on the camera you should know that settings is usually overwritten
on the main camera by the Cinemachine camera.
So now, no matter what you do on the main camera, the Cinemachine camera will control the main camera
and you need to do changes over here if you want them to work.
There are some settings that can be changed.
For example this one over here, but we don't really need to use it, so I guess that's irrelevant.
All right.
So main camera is controlled by the Cinemachine.
But what is cinemachine itself?
Well, uh, it may be, by the way, that your lens size is a bit different than mine.
Just make sure you set it to what you want.
And as example, I want to set value to six just like that.
Now we also need to focus camera on the player.
And for that we need to drag player to this field over here.
Track and target.
If you use previous version of unity like 22 or 21, you will have it say follow and you just drag your
player to the follow field and it's going to work fine.
Just same as mine.
Now, even like that, with the default settings, we're going to have nice and dynamic camera.
We can run around left and right, and you can see how alive the game feels.
So if you like the look of it, you can just use it like that.
You don't need to change anything.
Personally me, I want to see the entire level.
I want to have more of a precise platformer rather than fast paced platformer.
So I want to set up camera in a way that you can see entire level, plenty of actions, and then do
what you need.
But maybe for you it's different.
So let's talk about Cinemachine camera, and then I'm going to set it as I had before.
The first problem we can see here is that we can see outside of the level, this area over here, I
suppose it's not what we want to have in the game.
So we need to give this camera a limitations to give it a limitation.
We need to make an empty object.
Let's do create empty.
And I'm going to call it Cinemachine limits.
Nice.
Now for the limitations we need to give it a collider.
Um, in the previous versions of unity you should use the Polygon Collider.
So if you have 22 or 21, use Polygon Collider.
If you have later version just like I do, then we can easily use Box Collider 2D.
Let's place it in the center somewhere here, and then we can increase the size of the collider to something
like 25 by 25.
Then we can edit the collider, and this will define what player will be able to see.
Basically, the visible area will be only inside of this collider.
So you just need to decide what the player will see when he plays the game.
I think my player will be able to see this much, so only this area inside of the collider is visible.
Now, before we go ahead and apply this limitations to the camera, we need to disable collision of
this collider with any objects on the level.
We could do it through the layers, but that will take a bit of a time to set up.
So instead I want to just click this trigger over here and now nothing will collide with it unless you
have some specific functionality.
Now let's go ahead to Cinemachine camera and we need to click Add Extension Cinemachine container to
De.
Now we have a new component over here, and this will allow you to limit movements of the camera.
We can go ahead and just take cinemachine limits.
And drag it here to the bounding shape.
Now you'll see a message here, but it's not that important because once we start the game, it's going
to just disappear.
Let's go to play mode.
And there it is.
Now we cannot see what is outside of the level.
And still camera moves as before.
Very nice.
That's just what we need.
Yes.
Now let's talk a bit about settings on this machine over here.
First of all you can click save during play.
So any changes will be applied to the component even when you exit the play mode.
But I'm not going to do it because I want to explain things and then I want to have default settings.
Okay.
Now let's go through the settings one by one.
The most important one, and some are not so important like these two.
Camera distance defines how far your camera is from the object it follows and into deep perspective.
It's not important like it's not relevant.
You can just ignore it because it's only matter for 3D setup.
But we have orthographic view with a 2D camera, so we don't need to worry about this one as well as
the deadzone depth.
Usually it's used for 3D, so just ignore it.
There is also a screen position allows you to move a screen position as you like.
Could be useful for some 2D games, like if you've seen a Cuphead or very old game contra.
They have a view of something like this because you run from left to right, and you need to see all
of the enemies above you in front of you, so you could do some sort of.
Set up like this, and your character is always in the center of this point, so he can always see enemies
in front of him that could be used.
But for the platformer game, I don't think it's really a good idea because you need to see what is
below you when you jump.
So I'm going to set it to zero and zero.
All right, let's talk about something else.
Here.
We have a dead zone.
And that zone just gives a bit of a freedom to the movement of your character.
If you enable it, you'll see a small area around the character.
You can increase the area if you want.
Uh, maybe just like that.
And now when you move, your camera will not move.
I'm sorry.
I moved the position.
Yeah.
So your camera will not move until this yellow dot exits the dead zone area over here.
So you are free to move inside of this area before you apply cameras movement.
Now, if I jump outside of this area and my yellow dot will reach the limits, then I will move the
camera to the left or right, as you can see.
So it could be useful.
For example, you have a character that can do like this.
Then you could just set a very small dead zone.
And it would give you a bit of movement before camera moves, but if you don't have it, you can notice
that camera moves immediately once you do a small step.
Now there is a hard limit.
This one is more of a hard control of the camera.
I'm going to decrease the size as much as I can over here to explain what it does.
So, uh, usually Cinemachine follows.
Not the player itself, but this yellow dot over here, that's what it follows.
And this hard limit area will not allow this yellow dot to exceed the hard limits area.
So now when I move, I, it moves a bit because we don't have a dead zone.
But you can see no matter how hard I try, if my yellow dot reaches the limits, my camera moves immediately.
Right.
Now, of course it did exit the zone, but it's only because camera cannot move forward.
There is a limit of a collider, so that's kind of, uh, um, higher rule here.
But if camera can move freely inside of this area, you can see it follows the rule.
It does not allow this yellow dot to exit the area.
Of hard limits.
And here, for example, again you can see camera is on its limits.
It cannot go further.
But for example, if you disable this container over here.
Like that.
You can see now.
It never allows yellow dot to exit the area.
Okay.
Very good.
Now let's enable container again and let's disable this hard limits.
Now center on active is used when you switch between cameras or when you enable the camera basically
does what it says.
It will center on the object that it needs to follow.
And then there is a target offset.
So target is this yellow dot, and you can move the offset for this yellow dot to what you like.
That's all about personal preference.
And maybe there are some cases when it's needed.
Um, main reason I can think of is 3D view of a third person shooter, for example, because you want
your character to be somewhere here, but you cannot make camera follow the character, right?
You need to make it follow yellow dot that could be moved with the offset.
Yeah.
So that's what it does.
And dumping makes your camera move slower.
So let's just use it and I'll explain how it works.
Let's give it a dumping of 4x4 on x and y.
And now you can see when I move my camera is not so fast anymore.
It moves slower.
So it could be used for some cases.
Mostly I think about some maybe movement in the water or in the mountains where you need the slow movement
of the camera.
That could be done by not.
Um, but again, it's always up to the game design of the game.
Uh, for example, I had a top down shooter project and their camera followed the cross, the aim cross
of the player, not the character itself.
And when player moves his crosshair too fast, it makes it like it makes you feel dizzy.
For that purpose, we can use damping to slow down movement of the camera.
When you try to aim at different sides real quick.
Same could work for 2D top down shooter for example.
So I'm going to set it to one and there is last setting we need to talk about.
It is look ahead.
So if you increase look ahead.
Your camera will try to predict the movement of the character and it will look in front of the player.
Now it is a bit fast and it makes us dizzy again.
Like I cannot understand what is happening.
But if you increase smoothing over here, then it will be much easier to follow and see what is happening.
Okay.
So if you want to have smoothing, you could and you could also ignore why.
And it will do.
Look ahead only on the X, which is also pretty useful.
So these are the settings that are there.
And you can combine them to make the fill that you want.
But for me, for my project and this is what I suggest we do in this game, we will set up view of the
entire level.
So I'm going to go to the lens over here, and I'm going to increase it to something like maybe nine.
Or ten.
Yeah.
Let me try ten.
Okay.
So this is good.
Now I have a camera.
It looks same as before.
And you might ask me, why do we need a new machine if it looks same as before?
And I'm going to answer to you that we're just going to use a couple of nice techniques that we cannot
do without seeing machine so easily.
We're going to do camera shake and camera transition.
And I'm going to show you that in this section.
So let's save the project and let's go to the next video.