0
\$\begingroup\$

In the antique game spy vs. spy, two players play on a single computer. The screen is split, with each half showing the viewpoint of one player.

Is it possible to create such an effect in Unity? The main problem is that Camera.main is a global variable, so it is not clear how to have two cameras.

\$\endgroup\$
1
  • \$\begingroup\$ You know that no Unity police will force you to use Camera.main if you don't want to, right? You can absolutely store your own member variables pointing at two different cameras — what would stop you? \$\endgroup\$
    – DMGregory
    Feb 28, 2020 at 12:46

2 Answers 2

2
\$\begingroup\$

Yes, it's possible

Good answers linked above, but to bring the answer here (I didn't see a duplicate on SE):

You put a camera for each player in the scene. In your case, 2 cameras.

You then set each camera's "viewport" to your desired value: set Y to half (0~0.5 for P1, 0.5~1 for P2) for vertical, or X to half for horizontal. You can set it to a little less if you want a black border between the screens.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ If you want a border which isn't just black but looks a bit more fancy, then you might want to add a 3rd camera which only renders an UI canvas (controlled by camera culling masks) and place a separator image on that canvas. This canvas can also be used for any UI elements which are not supposed to be part of either player's screen. \$\endgroup\$
    – Philipp
    Feb 28, 2020 at 9:44
2
\$\begingroup\$

The viewport solution in the answer by Mars isn't bad, but I would do it a bit differently. My solution would be to use an UI canvas with two render textures:

  1. Add a UI canvas to your scene.
  2. Configure the main camera to render only the UI canvas by setting its culling mask to "UI" (the default layer of canvases and UI elements).
  3. Add two render textures to your project (one for each player)
  4. Add two additional cameras to the scene
  5. Make each of these two cameras a child of the player game objects, so they follow them around (for now - in the long run, you might want to develop a more sophisticated camera controller script)
  6. Set each of these cameras to render to one of these render textures.
  7. Add two RawImages to the UI canvas
  8. Assign the two render textures to these raw images

You can now freely design the layout of your game screen by moving the two player viewports around on your canvas and by adding other UI elements to it.

\$\endgroup\$
2
  • \$\begingroup\$ Good idea! I'm unfamiliar with the performance and quality of the render textures though--any differeces? \$\endgroup\$
    – Mars
    Feb 29, 2020 at 0:59
  • 1
    \$\begingroup\$ @Mars It should not be a problem unless you overuse them. Just make sure the render textures are in a resolution which is neither too small (which makes them blurry) or too large (which wastes render performance). In the ideal case you want the raw images on the canvas to be native size. \$\endgroup\$
    – Philipp
    Feb 29, 2020 at 16:27

You must log in to answer this question.

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