7
\$\begingroup\$

I´m finishing a game in Unity, but I have a new requirement: make it multiplayer. I will do this by splitting the screen and running two instances.

Is there a way to instance a scene twice at the same time, and render them two a different part of the screen? Alternativley, what is the best approach to achieve this?

\$\endgroup\$
11
\$\begingroup\$

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

VerticalSplitscreen

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

HorizontalSplitscreen


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 :

  1. Main camera with :

    • Correct viewport rect
    • A Depth value set to 0
    • A culling mask to Everything, except Player1Layer and Player2Layer
  2. A second camera with :

    • Correct viewport rect
    • A Depth value set to 1
    • A culling mask to Player1Layer (or Player2Layer for 2nd player)
  3. A dedicated canvas with :

    • Render Mode set to Screen Space - Camera
    • The correct camera configured in #2

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.

\$\endgroup\$
4
  • \$\begingroup\$ So far I've been unable to replicate the issue you describe below "Important Note." In my test, just having two cameras and two canvases, each set to "Screen Space - Camera" and mapped to the appropriate Render Camera, I see each player's UI displayed correctly in their half of the screen. Can you elaborate on what circumstances or issues require an additional camera per player? \$\endgroup\$
    – DMGregory
    Jan 4 '17 at 1:17
  • \$\begingroup\$ An additional camera is needed because when I tested, 3D objects were rendered in front of the UI when the camera was set to Screen Space - Camera. (This problem does not make sense since we are in screen space) \$\endgroup\$
    – Hellium
    Jan 4 '17 at 6:43
  • \$\begingroup\$ Did you try adjusting the plane distance on the canvas? This let me pull my UI in front of the objects without an extra camera. \$\endgroup\$
    – DMGregory
    Jan 4 '17 at 13:01
  • \$\begingroup\$ Yes I did, but 3D objects still may be rendered in front of your UI. Anyway, if my solution fits your needs, it's perfect ! :D \$\endgroup\$
    – Hellium
    Jan 4 '17 at 13:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .