ABSTRACT

Every Unity scene by default features one active camera, and this is the viewpoint from which everything is rendered when the game runs and enters that scene. For a GameObject to be a camera, it crucially needs a Camera component, which features all critical properties for a camera, including its Field of View and Background Color. In some cases, one camera may be all you need. But for most games, multiple cameras become both important and essential, for many different reasons. For example: if you need to render a user interface on top of an existing scene, or if you need an object to always appear in front of other objects, regardless of their real position in the scene, or if you need the ability to show other parts of the scene alongside the main game view, then you’ll probably need multiple cameras. And to work effectively with them, it’s important to understand camera depth. 1

Every camera has a Depth value. When multiple cameras are in the scene, the Depth value determines the render order of cameras. By default, cameras are rendered on top of each other like layers, with the lower depth cameras rendered before higher depth cameras. This means that a camera with a depth value of –1 will appear underneath a camera with a depth value of 0. Thus, if you add two cameras to a scene with the depth values of –1 and 0, and all their remaining properties are left at defaults, you’ll never see the rendered data from the –1 camera, even though it exists in the scene and renders, because a higherorder camera is rendered on top on each frame. 2

You can control how one camera is layered or blended over the camera below by using the Clear Flags field on the Camera component. This lets you layer camera renders on top of each other, making some parts of the render transparent, allowing the renders beneath to show through, like the layers option in Photoshop. Specifically, the Depth Only setting will hide the camera background, revealing any layers beneath. If you choose Depth Only for higher-ordered cameras, then pixels that don’t render geometry (like the scene background) will be rendered as transparent. Thus, the Depth field combined with the Clear Flags field means you can overlay entire areas of your scene on top of other cameras with alpha transparency, which is especially useful for rendering user interfaces and mouse cursors, making sure they render on top of everything else. 3

To test this, create a new scene with two separate cameras, one with a Depth value of –1, and another with a Depth value of 0 and a Clear Flags setting of Depth Only. Be sure to remove the Audio Listener component from the second camera, as Unity allows only one Audio Listener component to be active in the scene at any one time. Position the cameras

apart in the scene, and focus them on different objects, whether cubes or spheres or other meshes. Notice how renders of both cameras are composited together as one final output render in the Game tab, with the higher order camera rendered on top of the lower. 4

By default, every camera is configured to render all visible objects in the scene within its frustum, excluding nothing. Therefore, one issue that arises with layered, multiple cameras in the same scene is “double rendering.” This happens when two or more cameras can see the same objects, and so those objects are effectively rendered multiple

times, once from each camera. You can, of course, use this to create various special effects, but typically you’ll want to avoid it. One practical and “simple” way to do this is simply by positioning each camera, and the objects it should render, at the extremes of your scene away from each other. However, another way is to use Culling Masks to exclude the rendering of all objects attached to a specific layer from a specific camera. To achieve this, first mark all objects that should be hidden from a specific camera by attaching them to the same layer: select the object and assign it to a layer, picking a layer name from the Layer drop-down box in the Object Inspector. If no appropriate layer is available, you can create more by choosing Add Layer. 5

Finally, to exclude a layer from camera rendering, effectively hiding all objects on the layer from the selected camera, choose the layer name from the Culling Mask field in the Camera component. Alternatively, if you only want to render objects on that layer, then first choose Nothing to disable rendering from all layers, and then select the appropriate layer to enable it. 6

Split-screen games are typically local multiplayer games where the screen is divided into two halves, allowing each player to simultaneously control an independent character in the same world. That being said, split-screen has other purposes too: for single player games, for example, the player may activate a lever or button, and a small pop-up window

may display to show the effect that the lever press had on objects elsewhere in the world, such as a door-opening animation. Split-screen games clearly rely on multiple cameras in the scene, but their final presentation on-screen differs from the single, composited render in layers as discussed in the previous tip. Effectively, split-screen games should carve up the final render into distinct spaces inside where each camera renders its own output separately. To achieve this, you can use the Camera Viewport field. 7, 8

The Viewport field consists of a rectangle data structure with four members, defined in normalized space (between 0 and 1) representing the dimensions of the screen in each axis. This expresses the rectangular region inside which the camera is rendered on screen. 0 defines the left or bottom edge, and 1 defines the top or right edge. A value of (0.5 0.5), for example, represents the screen center. By default, every camera has X and Y set to 0 (bottom-left corner) and W and H to 1 (top-right corner), rendering the camera across the total surface of the screen. These values can be changed to render two cameras side by side in a split-screen format. To create a vertically divided split-screen setup, the left camera should be defined with the viewport of (X:0 Y:0, W:0.5 H:1) and the right camera should be (X:0.5, Y:0, W:0.5, H:1). 9

Alternative Applications: Mini Map with No Coding By using multiple cameras, combined with a creative use of the Depth, Clear Flags, and Culling Mask settings, as well as the Viewport Rect, you can achieve so many complex behaviors. One is real-time mini-map functionality, without having to write even one line of code! First, a mini-map is a game feature that often appears in real-time strategy games and RPG games. It offers an overhead view of the surrounding level and terrain, and features diagnostics colours and icons for items of interest, such as the player character, enemies, and collectable items. The mini-map is essentially an augmented reality HUD feature, one that helps the player navigate an environment and more easily make sense of what they’re doing and what is happening in-game. Here’s how you can quickly make a mini-map by combining all the tips we’ve just seen in this chapter. Start with an empty scene, and add a third-person controller from the Character Controllers package, as well as a terrain object, giving you a basic, traversable scene. 10

Next, add some sample enemy characters to the scene. For the test, they can simply be static boxes or any meshes. The important thing is that they’re independent game objects. Then add a new, secondary camera to the scene, ensuring its depth setting is higher than the main camera’s depth. This camera will act as the mini-map camera.