Hello guys.
Welcome to the video.
In this one we need to work on a co-op camera.
This means we will have to adjust the camera system quite a bit.
It may take long, maybe 15 to 20 minutes.
I'm not sure.
The testing took a lot.
So yeah, this.
What are we going to do in this video?
What the goal is.
Let's define the goal.
Our goal is to fit both players in the camera's view.
Also, we want it to be a bit dynamic and active.
And also we have a goal of making the camera follow one of the players.
If only one of the players are in the game right now, and then when there is two players in the game,
like when second player join in the game, then we need to center a camera between two players.
And also if two players are going too far from each other, we want to zoom out the camera so it can
see entire level and both players as well.
Okay, that sounds like a lot.
It's kind of a lot.
So let's get to business, okay.
First of all, we want to go ahead to this camera trigger and we want to disable these lines of code
over here just for now, because they will enable and disable camera whenever they want.
And we do need to do that.
As of now we want to have manual control of everything.
Also let's go to level camera and let's comment out this one.
Very good.
Now, first, uh, let's do something simple.
Let's zoom out the camera.
When players are getting too far from each other, or if, to be precise, when they getting out of
the view of the camera.
Uh, to show you what I mean by that, I'm going to make.
An object camera center point, and it's going to be exactly in the center of the level.
Now let's go to play mode.
I'm gonna make two players.
And let me take the camera and zoom in, maybe like eight.
And let me make it.
Follow the camera center point.
Okay.
So this is the center point.
And let's say one player goes here, he exits the area and second player goes somewhere here.
Right there.
I cannot get there anyway.
Now we want to zoom out the camera so both of them will stay in the view of the camera inside of the
frame.
How could we achieve that?
Well, I found a solution, I guess.
And before I show you that, I want to let you know that there is a component that's supposed to take
care of this, you can go to extensions over here and there is a group framing, but I do not like how
it works for me.
I feel like it's moving too much.
It just always shakes here and there, left and right.
And there are not many parameters to set up this thing, so I prefer to use custom solution that is
more suitable for our game.
This is what we're going to do.
We're going to make a new script.
Level camera view.
And we want to put Collider on the camera.
Box Collider 2D and let's make it is trigger over here.
Now let's adjust the size of a trigger, and it's going to be the frame that player are supposed to
be in before we change the view.
Let's say it's going to be 27 on the X and 18 on the Y.
I know because I tried, so I know the values.
Let's give offset on the y 2.2 and I want to see the box size.
Um looks okay.
Let's just try with this box size.
Then we can adjust it if we want to.
Now the goal is very simple.
We want to go to the script of Level camera view.
Let's put it on the cinemachine camera over here.
And inside.
We want to keep track of how many players are in the trigger at the moment, and if they're in the trigger,
it means they're in the camera view.
Okay, we could theoretically make the box size of a collider according to the size of the lens on the
cinemachine, but I think it's an overkill.
I don't really want to do it.
I'll just do manual setup.
So let me take the camera trigger.
I want to take these methods from here.
I'm going to copy go to Level Camera view.
Paste this in over here.
And then we need a variable just like we did in the last video private int.
Players in view.
Then over here we're going to do players in view Plus plus.
And if players in view is bigger or equals to level camera and we need player list here.
So I'm going to yeah we need player list here to know how many players there are supposed to be.
Well as of now let's use player manager.
All right.
I'm going to do player manager instance.
Get player list.
What's up with you?
Now we need to do dot count.
So if players in view are the same amount as it's supposed to be, we can decrease the size of the lengths.
We can take a closer look at the players.
So we need to decrease size of lens.
Then on the exit we want to do players in view minus minus.
And if players in view is less than player manager dot instance get player list.
Dot count.
Then we need to, uh, zoom out the camera.
So we need to increase the lens size.
Increase the lens size, and we could directly do level camera cinemachine and increase the lens size,
but that would be too snappy.
I want to have a smooth change of value.
That's why we're going to go to level camera and make a method that may look complicated, but actually
it's super simple.
All right.
Let's go ahead and make a value private float target lens size.
And I want to make it, uh, a serialized field because I want to show you how it works.
Then in the.
We're going to open up the gate and below let's make a method private void update lens size if needed.
Okay, now inside of this method we want to check what is the current lens size we want to do.
Float current lens size equals to Cinemachine lens orthographic size.
Now we have current land size.
We want to check if, uh, it is different from a target land size.
We can do that by just checking the difference and seeing if this difference is bigger than very small
value.
Also, if we just take the difference between two values, it's going to be either negative or positive.
But we want this check to work for both cases.
For that we need to take math F absolute math f absolute absolute will always return you a positive
value.
And that is just perfect for us.
Let's open a parenthesis and do current land size minus target land size.
And if positive value of the difference is bigger than 0.5 f, it means it is different and we need
to bring them closer to each other.
If to be precise, we want to change land size and make it equals to target land size.
We can do that smoothly with the help of a lerp.
We're going to do float.
New lens size equals to math f dot lerp from current land size to a target lens size.
And let's do time step of 0.5 f, multiply it by time dot delta time so it will be smooth and nice.
And then we want to take cinemachine lens orthographic size equals to new lens size.
And because of that it's going to smoothly increase or decrease the lens size.
You'll see.
Now also if difference is not very big, like if difference is very small, then we can just snap it
to the value that we want to have.
Cinemachine lens orthographic size equals to target lens size.
Okay, now let's update this value in the update.
Update lens size if needed.
I'm going to save it and I want to go to update.
I mean to unity and show you how it works.
Now we're going to go to Level Camera.
And let's say we know the land size now is nine, or actually it is ten.
Let's make it as nine.
And over here.
Target lenses by default is nine.
Now let's go to play mode.
And we can see camera is not moving, everything is fine.
And if we change the target value to ten.
It's going to zoom out just like that.
If you change target value to lower like five.
It's going to zoom in.
So this allows you to have method that changes zoom of the camera when you want to.
And now we can just change this value through the method to make camera zoomed in or zoomed out.
And that will depend on the amount of players we have in the camera's view.
Let's make a method public void change camera.
Lens size.
We're going to pass a float size and we're going to do target lens size equals to size.
Nice.
Now let's go back to this level camera view.
And over here we want to change value to uh.
Different one.
Well, we can hard code the value or we can make a variable.
That is up to you I think.
Yeah.
Let's do variables on a level camera.
Let's make public float mean camera size and public float max camera size.
Now I'm going to save it.
And I'm going to go back to level Camera view.
And over here we're going to do level camera.
Change camera lens size to level camera.
And here we zoom in in.
So I'm going to do mean camera size.
Over here we're going to do level camera change camera lens size to level camera max camera size okay.
This is supposed to work.
Let me remove this and let's go ahead and try this.
Oh, I forgot to change values.
Let's do a minimum camera size nine.
Maximum camera size ten.
Also, we don't need to see this value anymore.
We know how it works.
So I'm going to go back here I'm going to hide it.
And just in case, I'm going to go to awake and I'm going to do target lens size.
Equals to um cinemachine lens.
Orthographic size.
So by default it will take value that we have on the camera at the moment.
Okay.
So we don't have to change it every time.
Now I'm going to go to play mode.
Camera will look in the center.
I will add the player and then I'll try to walk out of the area.
The area is.
Where is it?
The area is over here.
This is the collider that is barely visible and I'll try to.
Jump out of that area.
I cannot jump there.
Well, I can go here and we can see that camera is zooming out slowly, which is very nice and very
pretty.
Now I'm going to go back and we zooming in so we can see both players.
Very cool, I love it.
Let me try to work out again.
And we zoom in out.
It feels a bit snappy.
I wonder why it should be smooth.
Maybe just because of the structure of the level.
Well, it doesn't really matter because we're going to adjust it more.
And that will fix the problem with the shaking over there like we see right now.
Okay, so what is the next goal?
Now we cannot keep camera just in the center like that.
I want it to be a bit dynamic.
I want it to move around.
And for that we can place camera.
Center between two points if there are two players in the game.
If we want to do that.
We need to go to level camera, and we need to check how many players there are in the game right now.
Um, we can make a private list of player.
Player list.
And let's let's maybe make it public.
Public yet private set.
Now we can use this list on a level camera view over here instead of player manager.
Mobile camera player list dot count.
Same goes here.
The on camera player list count.
Nice.
And then let's go to level camera and adjust it slightly.
First of all, we want to have the center point.
So I'm going to make satellites field private.
Transform camera center point.
And this is the point we're going to place between two players.
Now let's go and make a method that would update camera status.
And we can update camera status every time player joins or disconnects from the game.
I'm going to make a method over here.
Private void update camera status in which we want to update, uh, list.
So I'm going to do player list equals to player manager.
That instance get player list.
Then we want to see if player list count is bigger than one, means we have more than one player in
the game.
And if that's the case then we want to focus camera on a center point.
I'm going to set new target camera center point.
Also you can see we have a misspelling over there.
So I'm going to fix it.
Then else means if we don't have more than one player in the game, we're going to focus camera on the
first player in the list.
Set new target layer list with a zero dot transform very well.
So this will update the camera status.
We just need to call this method.
Uh, like at the right moment of the game.
I think it's better to do this when player dies or respawns.
So I'm going to go all the way below over here.
And I'm going to do on enable.
Player manager.
Uh.
On player.
Respawn.
Plus equals.
Update.
Camera status.
Player manager on player death update camera status.
Also we need to unsubscribe from this.
So on disable.
Let's take these two lines of code.
Paste them in here.
And do.
Minus.
Okay, let's save this.
Uh, let's go back.
And we could call it in the update.
And this supposed to do half of the job.
We also want to find a midpoint between two players and place this center point between two players.
Actually, it's not that difficult to do.
Let's make a method.
Public void update center point position.
And over here we just want to get transform of two players.
Find the midpoint between them and then place camera there.
To do that we need to have reference to players transform and we could get them from the list.
But I want to have a clear variable so we understand what it does and why we use it like that.
So I'm going to do private transform P1 private transform P2.
Let's do.
Player one.
Player two.
Okay.
Now we're going to go to this.
Oh, and also I want to have a boolean that says private bool camera focused on center.
Now let's go to this method.
And if we have more than one player in the game and we updated the list, then we want to do player
one equals to player list zero transform player two player list.
One transform and camera focused on center.
True.
If you don't have the players camera focused on center.
False.
Very nice.
Let's save this and let's go back to this method.
First of all, if camera focused on center is equals to false, we're going to return from here.
So we're not going to update center position.
And if we do then we need to do Vector3 midpoint.
And this will be middle point between two players.
We can easily get it by typing player one.
Dot position plus player two dot position divided by two, and this will give us a middle point between
two objects.
And now we can take a camera center point dot position equals to midpoint.
Now the goal is to update position at all times.
But I think I don't want to do this in the update.
That could be too often.
So let's go to awake over here and let's invoke this method with repeating.
Invoke repeating.
You're going to do name of update camera center point position.
And zero delay.
Repeat rate.
Point one F.
This is pretty often, but not too often.
Okay, so yeah, it was kind of fast.
I'm sorry if it was fast.
I just, you know, I'm going to tell you a secret.
I'm recording this video for the fourth time's.
Yeah, I did try 4 or 5 times and I couldn't get the flow right where I am explaining everything in
a needed and needed order.
So I had to start over again and again.
Again and again.
And that might be why I'm going a little bit fast here.
I hope you can forgive me for that, especially in the end of the course.
Yeah.
By the way, congratulations.
We are finishing up soon.
Now let's go back to unity.
And we have this camera center point over here.
So I'm going to just assign it into this field.
And I want to take center point.
And I want to give it a gizmo so I can see it.
Now this is supposed to do the job.
Let's go to play mode.
There is one player.
Very nice.
It is focused on the player and it feels pretty dynamic, I like it.
Now I'm going to add another player and now camera is between them.
We can enable gizmos and we can see the dot over here.
If I go there and if I.
Maybe jump over the player camera.
Going to be dynamic.
It's going to move to the sides.
Very nice I love it.
And.
Yeah, I got here.
Now, let me try to walk away from the player and it's supposed to zoom out.
Yeah, it did zoom out.
Very nice and clean.
Almost unnoticeable really.
We can see two players in the level.
And if I come closer, it's going to nicely zoom in.
Also, you probably noticed that just now I could see a bit of a, uh, blue background over here.
It may happen sometimes because camera, you know, it's very difficult to manage.
There are so many things considered.
And what you could do is just draw the level around the playable area over here.
Just add some parts to hide it.
Just like that.
Also, you could go to, um.
Main camera over here.
Find the background and take Color Picker and choose background.
Uh, just like this one.
Let's just click it on it.
Okay now in the game, even if you don't have area there.
It will look just like a background of the level.
Let me show you that I'm going to just.
Remove here everything.
Here you can see it looks dark blue, so it's fine.
So if it is outside of the level, it's going to just show you this background and it does not look
like a bug.
Okay.
Very well.
Now.
We did a lot.
I can see it's about 20 minutes now, but we still have to do one more adjustment here.
And to do this adjustment, we better go.
We better go to another level.
Now let's take camera manager level camera.
Let's apply overrides.
Apply.
So now this camera has everything that is needed to make it work.
And we can go to scene level four.
I'm going to save it.
And on this level we have two areas.
Area one and area two.
Now I don't want one of the players to go further without second player joining him.
If there is only one player entered next room, then he's supposed to wait for the first player to enter.
This is what we need to do, how it's going to be.
We're going to place invisible colliders.
The player cannot pass by and we're going to disable this colliders only when two players entered the
room.
The rom rom.
Yeah.
So this one we're going to do we're going to go.
We're going to go.
Let's say here.
Let's make 2D object sprite square.
I'm going to name it as, uh, maybe.
Level limits.
Not sure it's supposed to be under the camera, but this is a pretty good place for it because functionality
going to be here.
And we're going to just take these limits and let's say place it somewhere.
Here.
Let's do player minus one so I can see it.
And I'm going to just.
Make it white like that.
Also let me take checkpoint and move it here.
So now we have this level limit.
It is a collider.
We want to add box Collider 2D here and we want it to be unpassable only for players.
So let's make a layer.
Layers.
Limits.
We're going to go to Edit Project Settings.
And over here we need to disable collision for everything but a player.
Nice.
Gonna close it.
And then we need to go to.
I'm sorry.
I need to think a bit.
We need to go to level camera because I think we can centralize everything and then just call method
from here.
And we want to make array.
Serialize field.
Private.
I'm going to do it here.
Collider 2D array limits.
Then we want to make a method that will disable or enable these limits.
So.
What are we going to do?
Over here we have enable camera.
I'm also going to do a public void enable limits.
Bool enable, then we're going to do for each var collider in colliders are in limits we're going to
do Collider.
Collider enabled.
Enable.
So this one can disable or enable limits.
Also, since we're doing this, let's make a variable that says that camera is enabled.
I'm going to do.
Right.
Red bull camera active maybe like that.
And I want to disable all of the functionality if camera is not active.
So if camera active.
False return then over here as well if camera active.
False.
Return and.
Over here as well.
If camera active polls return.
Nice.
And this one should be okay.
We don't need to update this one.
This one will not be called because we need to return in the update.
We just need to go here and do camera active equals to enable.
Nice.
Now let's go back to level camera trigger.
The one we had before.
It is over here.
And we did enable camera if player entered the area.
And then we disable the camera.
If player exit the trigger.
Now we want to disable the camera only if two players are exiting the trigger or two players entering
the trigger, and actually the amount of players that is supposed to be in the game right now.
So this is what I'm going to do.
I'm going to make private int.
Players in.
Trigger.
Then every time somebody enters the trigger, we're going to do player sin, trigger plus plus.
Then we want to open if statement.
And inside of this if statement we're going to do if player sin trigger is equals to level camera player
list dot count then level camera enable.
Tamara.
True.
Then we need level camera enable limits.
True because we want to block exits out of this room.
Then this one can be removed and we can go over here and do player scene trigger minus, minus.
And then if player since trigger is equals to zero means there is no players in the room anymore, we
can, uh, unlock the doors and we can disable the camera.
So I'm going to do level camera, enable camera.
Balls and level camera enable limits.
False.
Okay, let's save this.
Let's go back to unity.
And I'm going to have a level limit over here.
For this room.
And this one basically means that we cannot exit from here until there is two players on the level.
Okay, that's the idea.
Uh, something like this should be fine.
Okay.
And then for this camera over here, let me duplicate it.
Actually, I think we can just.
Apply overrides.
Now this one has level limit.
I'm going to enable camera.
I'm going to take level limit.
And I'm going to place it over here.
So we cannot go back alone.
We can go back only together.
You getting the point right for this area.
Exit is blocked over here for this area.
Exit is blocked over here.
And if you have more exits, you can just add another game object and assign it into the.
Array over here.
Let me assign it here.
And for this room should be here.
Okay.
Very good.
Now everything's supposed to work.
I think we did pretty much everything that we need.
And let's go and try this.
Can I go to game mode?
Going to go to play mode.
Oh, wait a second, I think I forgot something.
We need to take level limits and we need to change layer to players limit.
Now I'm going to apply overrides again.
Let's enable second camera.
That looks good to me.
Now let's go to play mode.
Also why it is showing this view right now.
Let me do minus one over here so it does not bother me.
Okay.
So it has less priority.
Now let's go to play mode.
I'm going to join the player, and I can see that camera did not focus on the player.
Okay.
Maybe I made a mistake.
Let's go to level camera.
Uh.
Over here.
Let's not return when we update camera status.
I think this what breaks.
The deal.
Going to save it?
I'm going to try again.
Let's go to Game View and let's go to play mode.
All right.
There is player one.
Camera follows the player one.
Very nice.
Now let's make a second one.
And there is a second player.
Okay.
Very good.
Now let's try to walk here.
Camera zooming out.
I like it so we can see both of them.
Now let me get the second layer there.
I know Mushroom Hill.
Now.
First player goes here.
Oh.
It should be disabled by default.
Now let me go there.
Let me take level limits.
Let me disable it.
I just disabled the door from a second level because this one should be disabled by default.
Okay, so I can go there, but I cannot go further.
I can only return.
This is good.
Now let's try to walk there together.
And yes, we did change the view to another level.
This is all good.
And also, if we want we can walk back.
Let's go by the.
And both of us here.
Okay.
Very nice.
It works, I love it, I love the system.
This looks so cool and feels so nice.
Uh, let's.
Go to level camera and in the wake I'm going to disable level limits.
Wait.
Enable limits.
Polls.
So we're going to disable limits in their wake, but then we're going to enable them when player is
joining the game.
Right.
Okay, I think that's it.
Now let's take these limits.
Let's make them transparent.
Just like that, so it's not visible.
And let's go to the next video.
Yay!