Hello guys.
Welcome to the video.
In this one I want to show you how to set up the split screen.
It is purely optional.
I don't want to have this functionality in my version of the game, but I want to show you how to do
that.
So if you want, you can use it.
It's pretty much fine.
It's fun to play with, and I think it is as good as the system that we made together.
But just my personal preference for this kind of game is not to have a split screen, but joint camera
for two players.
Nevertheless, I want to show you how to make the system and I'll keep it in the project.
You can use it if you want, but I'm not going to.
Okay.
So how do we make it?
Uh, let's go to level one, and I'm going to use level one as example to explain how do we set up split
screen camera before we set up split screen camera we need to remove functionality that we have on the
level camera.
Uh, sadly for me, the split screen does not require as many things as we did need for this setup,
so we'll have to remove it.
Let's take this level camera and let's make an impact completely on the prefab.
We want to remove this component over here.
We want to go to level limits and delete them.
We want to take camera center point and assign it to Cinemachine camera over here in this field.
Over here in this field.
Let's do lens ten.
And this is going to be the camera that, uh, player use until he joins the game.
Like the first look of the level before you press any key to join.
Then on cinemachine limits, we don't need this component.
I'm going to remove it.
And on Cinemachine camera looks good to me.
This level camera view can be removed.
And.
Yeah.
Okay, let me cover all of this box.
Collider can be removed.
And this should be good.
Now what do we need to do?
This player manager has a functionality enables split screen.
Lets enable it.
Also there is a set fixed number of cameras you can set to two.
But I want to show you the reason why we need to enable it.
So for now I'm going to disable it and you'll see later.
And then we're going to use it okay.
Um.
Now how it's going to be.
We need to make a setup where we can store main camera and cinemachine camera for each player, because
this is how it's supposed to be.
We're going to have two main cameras apart from this one, and these two main cameras will be attached
to two new different cinemachine brains.
Each camera will take half of the screen and it will follow the player because it will have cinemachine
brain.
Okay, this what we need to do.
So first of all, we need to make two new city machines, two new main cameras, and then we need to
save them in some variable.
So let's go to script.
Camera.
I'm going to make a script.
Level split screen setup.
On the script.
We're going to make a public camera array, main camera and then public cinemachine camera.
Cinemachine camera and this should be an array I forgot.
Okay.
Like that.
Very good.
Now let's save this.
Let's go back.
And we need to attach this component over here.
And now we need to fill up these variables with cameras.
Okay.
Let's take Cinemachine camera from here.
Let's duplicate it twice.
And let's give it a name with a underscore one and underscore two just like that.
Also let's make sure they have container and they do okay.
This is fine.
Now let's go to level camera.
Let's drag Cinemachine and Cinemachine two.
Very nice.
Now we need two main cameras I'm going to make a camera.
Going to name it as camera one.
Duplicate it.
Camera two I'm going to bring them up over here as well as these two over here.
And then let's take main camera and copy component.
Go to these two cameras and paste component values.
Very nice.
Now there is something you need to know.
There is this audio listener, and you're supposed to have only one audio listener in the game.
That's what you're supposed to have.
And we can remove the audio listener from the second one.
But we cannot remove the audio listener from the first one, because when we enable split screen, we're
going to disable main camera over here.
And still we need to have at least one audio listener in the game.
Okay.
That's why we're going to keep it here.
All right.
So once again, camera one has audio.
Listener camera two does not have audio listener.
Cool.
Also, I want to be sure that at the start of the game, main camera shows the view of the level and
it has higher priority than these two.
To do that, we can just take main camera and change depth to 0 or 1, and now it will have higher priority
over the main camera.
Another thing that we must do to enable split screen is to attach cameras to different cinemachine brains.
Let's select this tool and add component of a Cinemachine brain.
And you can see that both of them are connected to Cinemachine camera two, as well as the main camera
over here.
What we need to do is to make this camera be connected to this one.
Camera one should be connected to the first one.
Camera two should be connected to the second one like that.
That is very easy to do.
We just need to take cameras over here.
Go to Cinemachine brain fine channel mask.
Choose nothing.
Then for the first one we should have channel one.
For the second one, we should have channel two.
Very nice.
And for Cinemachine camera over here, we need to open the camera, find output channel, do nothing.
Channel one for second.
Camera nothing.
Channel two.
Now if you select this camera, you can see it is connected to Cinemachine camera two and this one is
connected to Cinemachine camera one.
Now let's go to the cinema screen over here.
Output channel.
And this is fine.
We can leave it as it is on the main camera on Cinemachine brain.
We need to find channel mask and remove channel one and remove channel two.
So it should be like that.
Very nice.
Now this one is connected to this one to Cinemachine camera.
Now this one connected to main Cinemachine camera one connected to the first cinemachine.
Camera two connected to the second Cinemachine.
Very good.
Let's go to level camera and drag this over here.
Like that.
And also we want to disable main camera once we enable split screen.
For that, I have an idea.
We can just make a script object to disable.
Inside of this script, I'm going to open Start Method and I'm going to do player manager dot instance
objects to disable and this game object.
So it will add itself to the list.
And then player manager will disable this game object.
And now we can just put the script on the main camera.
Like sorry.
Like that.
Nice.
Very nice.
So we did almost everything that we need to do to make split screen work.
And now we can set up this manually and it will work.
But I don't want to set up it manually.
I want player manager to be responsible for enabling split screen.
And it will do that.
You just need to know that when it creates a player to make a split screen, it needs to go to the player
input component over here and assign camera in this field.
So this one we need to do in the script.
Let's go to player manager script.
We need to go to add player method.
Over here I'm going to do.
If player input manager split screen is equals to true.
Then we need to take the camera from split screen setup and assign it into component of a player input
with the name New player.
And the only problem is we don't have access to this variable yet.
So we need to go maybe here and make a private level split screen setup.
Split screen setup.
And we need to get this component.
And it should be found on a new level every time.
So I'm going to go.
Maybe here.
This is our kind of start method.
Right.
And I'm going to do a split screen setup equal to find first object by type of a level split screen
setup.
Just like that.
Very nice.
Love it.
Now let's go back over here and let's do this.
New player camera equals to split screen setup.
Main camera with an index of a new player number.
Then we're going to take a split screen setup cinemachine camera with an index new player number and
we're going to make it follow new player transform.
And that's it.
Let's save this.
Let's go back to unity.
Uh, we have split screen enabled over here, so it should be good.
Let me try.
Maybe I forgot something.
So there is the camera view.
It says press any key to join.
I'm going to join the game.
There is a first player and it says camera was not associated with the player.
I know it's fine.
Now let me join the second one and we have a split screen.
First camera follows the first layer.
Second camera follows the second layer.
The only problem is that wait, it does not follow.
Oh it does.
Yeah.
The problem is that player is not really in the center.
So what we can do here is go to camera two.
Open this over here.
And.
Take the X over here and make it 0.5 like that.
Very nice.
And I guess if we use a split screen setup for the game, maybe it is safer to remove audio listener
from here.
So I'm going to do that or I'll just disable it like that okay.
Good.
Uh, let me try again.
So there is a single player in the game.
Looks good to me.
Let me check if camera follows it.
Yeah, it follows the player.
Very nice.
Maybe we can disable this camera as well, because we don't need to, like, we don't need it to work
for nothing.
But I'll do that later and let me join the second player.
And now we've got a split screen for the second player.
And player is in the center as well as this one.
Uh, if you like, you can make view a bit closer.
Just over here.
Choose lens type seven and maybe eight.
Yeah, and it's a bit better.
So now camera is following one player and second camera follows the second player.
Now what happens if second player dies.
This what happens.
Well let me kill him.
So when I kill this player the camera will become normal again.
And I don't really like it.
Especially because if you kill the first player.
You will have split screen enabled anyway, so it's kind of not fair to fix it.
We can go to.
We can go to player manager.
Choose set fixed number and set number two.
Now we will always have split screen.
Very good.
And the camera's over here.
You can do lens size eight.
You can add object to disable on this component.
And basically that's it.
Let me try again.
First layer, now second layer.
And now we have split screen.
And if first layer goes and.
Gets him killed, then we still have a split screen and the camera will wait for you to join the game
again.
Ta da!
We're here.
So yeah, magic is done.
I'm not sure if there is anything else to show.
Maybe I'll take.
Let me cancel it.
Yeah, this one is.
It's okay.
Uh, let me go to asset prefab camera.
I'm going to drag this over here.
Maybe it would be even better to.
Yeah.
You know what?
Let's.
Take component from here.
Put it here then.
Let's take this component here.
Put it here.
And let's take this main camera.
Copy it, paste it in, move it.
Here I'm going to just do main camera like that.
Or maybe camera zero.
And now we can remove this game object from the scene because we don't need it anymore.
And we can make a prefab.
Camera.
We can take this level.
Camera.
Change the name to um.
Split screen setup.
And we can drag it here as a prefab.
And now if you want to have split screen in the camera, you just go to level.
You remove camera manager, you remove level camera and you drag and drop these split screen setup and
it will work.
As for me, as of now I have a prefab.
I'll just hold Ctrl Z to remove all of the changes that I did.
All right.
Very good.
So I have the version of the game as it was before without a split screen.
But prefab is here and if you want you can use it in your game okay in the script as well.
It will work only if you enable split screen on the player manager, so it's fine.
Okay, now I'm going to close everything.
I'm going to save the project and let's go to the next video.