- Home /
Split the screen for multiplayer
I´m finishing a game development, but I have a new requirement, make it multiplayer (splitting the screen and running two instances).. There is a way to instance a scene at the same time, and render them in a different part of the screen? Or what is the best approach to achieve this?
Thanks in advance!
Answer by Hellium · Jan 04, 2017 at 09:56 AM
I will post here the same answer I gave you on StackExchange
If you want to make a local multiplayer with a split screen, the way to go is to have two cameras and change the rect viewport of them :
https://docs.unity3d.com/ScriptReference/Camera-rect.html
For vertical split screen : (x, y, width, height)
1st camera : (0, 0, 0.5, 1) ► left
2nd camera : (0.5, 0, 0.5, 1) ► right
For horizontal split screen : (x, y, width, height)
1st camera : (0, 0.5, 1, 0.5) ► top
2nd camera : (0, 0, 1, 0.5) ► bottom
No need to have two worlds. However, if you want the players to have their own GUI, you will have to take a look at culling masks
https://docs.unity3d.com/ScriptReference/Camera-cullingMask.html
Place all the objects of the player 1 in a new layer ("Player1Layer"), all the objects of player 2 in another ("Player2Layer"), and make sure the camera of the player 1 does not have "Player2Layer" checked in its culling mask while the camera of the player 2 does not have "Player1Layer" checked in its culling mask.
IMPORTANT NOTE
It seems that additional configuration is required so as to show GUI to a specific player. I don't really know why.
Each player must have their own :
Main camera with :
- Correct viewport rect
A
Depth
value set to 0A culling mask to
Everything
, exceptPlayer1Layer
andPlayer2Layer
- Correct viewport rect
A second camera with :
- Correct viewport rect
A
Depth
value set to 1A culling mask to
Player1Layer
(orPlayer2Layer
for 2nd player)
- Correct viewport rect
A dedicated canvas with :
- Render Mode set to Screen Space - Camera
The correct camera configured in #2
- Render Mode set to Screen Space - Camera
For the sake of simplicity, attach the two cameras of a given player to a unique parent, and move the parent so that the two cameras will follow. Maybe, you can keep the cameras #2 at (0, 0, 0). I haven't tested.
I know this is a bit old but i was wondering if it's possible to make a split screen with irregular shapes and more than one camera?
I guess you can, but not with this technique.
$$anonymous$$aybe, you can use a RenderTexture
to render what the cameras see, and "crop" the output with the $$anonymous$$ask
component or with the Graphics.Blit
function. Before blitting the texture, you could change the Texture by using SetPixels
(not really advised), or with a cutom shader I guess.