Cameras

In Unity 5

Good evening gents and ladies.  Today’s topic is cameras.  What are they used for and what can they do.  I’m going to cover some intermediate lessons inside this tutorial, so let’s get started.

camera screen

The image above is a camera in scene view.  Every new scene starts with a camera, among other things.  The camera is your view port in game that allows you to see the objects in the scene while the game is playing.  A camera may be moved and rotated to capture any image you’d like.  Generally, it’s a good idea to decide early on whether you want it to be orthographic or perspective.  However, you basically chose this when you clicked 2D or 3D when you started a new project.

A couple of notes on the camera inspector…
  • Clear flags are useful.  You may clear to a Sky Box, which means that whatever Sky Box you have available in your scene (usually 3D games) will become to default background of your game.  Alternatively, you may clear to a default color.  There are other clear options, but they generally aren’t used in most cases.
  • Culling mask is useful if there are some elements in the scene you don’t want your camera to capture.  For instance, if you have a UI elements for a mini-map camera that you don’t want your main camera to pick up, then you might check them off here.
  • Most of the other settings can be left as they are.  In most cases, you’ll never need to change them.
  • A camera also comes with a built in audio listener.  This component is what picks up any playing sounds attached to objects in the scene.  Audio listeners function in real space, so I always found it a good idea to place any Background music close to the main camera.  If the main camera tends to move in the scene, then make a BGM game object that plays the music and make it a child of the main camera.
Useful Tips
  • You can have any number of cameras in a scene, but depending on how they are layered, some may draw over others.  To set the order in which your camera’s draw, consider modifying a camera object’s depth variable in it’s inspector.  Cameras will render in order of depth.
  • A good way to make a camera follow a player object is to make it a child of the player.  If you have a sprite that moves according to user input via script, as in a character controller, the camera will move with that sprite.  A word of warning, however.  All transform values will conform to the player, including rotation.  If you intend on making your camera follow the position of the player object, but not rotate when he does, then you may want to consider a follow object script that makes camera’s position conform to a target game object.
Making Mini-Maps

This feature is available in Unity 5.  Otherwise, you may access this feature in any Pro version of the software.  While it is possible to create a mini-map in a non-pro version of Unity, the metrics are tricky and it will not conform to multiple resolutions.

new Camera

To create a mini-map, you first need to add a second camera.  Let’s call this a mini-map camera and place it high and looking down.  To create a new camera, you can right click in the hierarchy and select Camera.  You may uncheck as many layers in the culling mask as you see fit.  If you want a clear areal view, then leave it the way it is.

Next, create a render texture.  To do so, go to the project tree.  Right click and go to Create > Render Texture.  Then, drag that file into your mini-map camera’s inspector.  There will be an option called target texture in one of the camera’s components.  So far so good.

render texture

The final step will be to create a UI element and place the target texture into it.  You can do this by right clicking in the hierarchy and going to UI > RawImage.  This will automatically set up a canvas object and place a blank texture inside.  Drag your render texture that you just dragged into the camera’s inspector into this object’s inspector. Done.  Start the game and you will see everything your minimap captures placed into this canvas object.  You may experiment with the layout and size dimensions by fitting the canvas object around the canvas.

That’s all for now!  Join me next time for another tutorial.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s