< Return to Video

Cameras - Unity Official Tutorials

  • 0:00 - 0:02
    Cameras.
  • 0:02 - 0:03
    The camera is one of the most
  • 0:03 - 0:05
    essential components in Unity.
  • 0:05 - 0:07
    The camera takes the contents of our scene
  • 0:07 - 0:09
    and displays it to our users.
  • 0:09 - 0:10
    Every scene must have at least
  • 0:10 - 0:13
    one camera to render out scene objects
  • 0:13 - 0:15
    otherwise we have nothing to show.
  • 0:17 - 0:18
    When a new scene is created
  • 0:18 - 0:20
    one game object is always created.
  • 0:20 - 0:22
    This is the main camera.
  • 0:22 - 0:24
    The game view camera is a
  • 0:24 - 0:25
    component attached to a game object.
  • 0:26 - 0:28
    This means we can manipulate, or move our camera
  • 0:28 - 0:29
    like any other game object,
  • 0:29 - 0:31
    including parenting, scripting,
  • 0:31 - 0:33
    or physical interaction.
  • 0:35 - 0:37
    To create a first or third person camera,
  • 0:37 - 0:39
    including side scrollers,
  • 0:39 - 0:42
    we can use the player object as the parent.
  • 0:44 - 0:46
    For first person cameras, make sure the camera
  • 0:46 - 0:48
    is at the character's eye height
  • 0:48 - 0:51
    looking forward from the character's point of view.
  • 1:02 - 1:05
    For a third person view, make sure the camera is
  • 1:05 - 1:08
    above and behind the character.
  • 1:18 - 1:21
    For a simple puzzle game or top-down shooter
  • 1:21 - 1:23
    the camera would be static, simply
  • 1:23 - 1:25
    looking at and rendering the game.
  • 1:25 - 1:28
    In this example we are going to centre the camera,
  • 1:28 - 1:30
    remove any unwanted rotation,
  • 1:30 - 1:32
    point it straight down and lift it above
  • 1:32 - 1:35
    the game board to simulate a top-down game.
  • 1:36 - 1:39
    In this case we are using the orthographic
  • 1:39 - 1:40
    mode on the camera, which we will cover
  • 1:40 - 1:42
    later in this lesson.
  • 1:52 - 1:54
    We can have any number of cameras in our scene.
  • 1:54 - 1:57
    Each rendering different parts of the environment.
  • 1:58 - 2:00
    In this example we have three cameras.
  • 2:01 - 2:02
    One rendering all of the dynamic
  • 2:02 - 2:05
    objects in the scene. Another rendering
  • 2:05 - 2:07
    the static background and a third
  • 2:07 - 2:09
    rendering a User Interface overlay.
  • 2:10 - 2:12
    All three cameras can be brought together to
  • 2:12 - 2:14
    make a single presentation to our user.
  • 2:15 - 2:17
    We will talk about how to properly use all
  • 2:17 - 2:19
    three cameras at once later on in this lesson.
  • 2:20 - 2:22
    When a camera is selected in the hierarchy
  • 2:22 - 2:25
    we see a preview of the camera in the scene.
  • 2:25 - 2:27
    When we have multiple cameras in the scene
  • 2:27 - 2:30
    this helps us to see what the camera is rendering.
  • 2:30 - 2:32
    This preview is also helpful when we are in
  • 2:32 - 2:34
    full screen mode to see what the camera is rendering,
  • 2:34 - 2:37
    even if it's the only camera in the scene.
  • 2:41 - 2:43
    Cameras will render everything that's in
  • 2:43 - 2:45
    front of them and within their view.
  • 2:45 - 2:47
    How much of the scene is within their view
  • 2:47 - 2:50
    is shown in the scene view as a white outline.
  • 2:50 - 2:53
    This shape is a view frustum.
  • 2:53 - 2:56
    A view frustum is a pyramid, or cone,
  • 2:56 - 2:57
    with the top cut off.
  • 2:57 - 3:00
    The cut off top of the pyramid is the
  • 3:00 - 3:02
    near clipping plane and the base
  • 3:02 - 3:03
    is the far clipping plane.
  • 3:04 - 3:06
    The near and far clipping planes control
  • 3:06 - 3:08
    the draw distance from the camera.
  • 3:09 - 3:11
    Objects must be between the near and far
  • 3:11 - 3:13
    clipping planes to be rendered. The sides
  • 3:13 - 3:15
    indicate how much the camera can see
  • 3:15 - 3:17
    side to side and top to bottom.
  • 3:17 - 3:19
    and any part of the scene that's within
  • 3:19 - 3:21
    the frustum will be rendered.
  • 3:23 - 3:24
    Cameras have two different ways of
  • 3:24 - 3:26
    looking at the scene. Perspective mode
  • 3:26 - 3:28
    and orthographic mode.
  • 3:28 - 3:30
    These dramatically effect the shape and
  • 3:30 - 3:31
    size of the frustum and the
  • 3:31 - 3:33
    look of the scene through the camera.
  • 3:34 - 3:36
    In perspective mode the camera will render
  • 3:36 - 3:38
    the scene like a real world camera with
  • 3:38 - 3:40
    a sense of diminishing perspective.
  • 3:40 - 3:42
    We can see this in the scene view as the
  • 3:42 - 3:44
    white representation of the cameras
  • 3:44 - 3:46
    frustum gets larger as it extends
  • 3:46 - 3:48
    away from the camera.
  • 3:48 - 3:50
    This is the most common camera mode to use
  • 3:50 - 3:51
    when creating a game.
  • 3:52 - 3:54
    In orthographic mode there is no
  • 3:54 - 3:56
    diminishing perspective. All objects are
  • 3:56 - 3:57
    rendered using a form of parallel
  • 3:57 - 4:00
    projection from the camera. We can see this
  • 4:00 - 4:02
    in the scene view as the frustum is straight
  • 4:02 - 4:05
    and the front and back are the same size.
  • 4:05 - 4:07
    This mode is usually seen in isometric
  • 4:07 - 4:09
    games like some real time strategy or
  • 4:09 - 4:11
    board games, or for 2D
  • 4:11 - 4:13
    games, simple puzzle games and when using
  • 4:13 - 4:15
    an additional camera for rendering UI elements
  • 4:15 - 4:17
    on top of the game view, like mini
  • 4:17 - 4:19
    maps or heads-up displays.
  • 4:20 - 4:21
    To control what is being rendered in our
  • 4:21 - 4:24
    scenes, adjust the near and far clipping
  • 4:24 - 4:26
    planes and the size or shape of the frustum.
  • 4:27 - 4:29
    Field Of View controls how wide the view
  • 4:29 - 4:31
    of the camera will be. This is very much
  • 4:31 - 4:34
    like using the zoom on a real world camera.
  • 4:34 - 4:36
    When the camera is in orthographic mode
  • 4:36 - 4:39
    size replaces the field of view property.
  • 4:39 - 4:40
    This controls the size of the
  • 4:40 - 4:42
    orthographic viewport.
  • 4:42 - 4:45
    This is similar to field of view but
  • 4:45 - 4:47
    the value of the size property changes
  • 4:47 - 4:49
    the size of both the front and back planes
  • 4:49 - 4:51
    at the same time as there is no perspective
  • 4:51 - 4:53
    with an orthographic camera.
  • 4:55 - 4:57
    Our scenes must have some sort of a background.
  • 4:59 - 5:01
    This controlled by the Clear Flags and
  • 5:01 - 5:02
    Background properties.
  • 5:02 - 5:04
    The colour values set in the background
  • 5:04 - 5:06
    property will be what's drawn behind any
  • 5:06 - 5:08
    of the objects in our scene, if no other
  • 5:08 - 5:11
    settings have been changed. This is the
  • 5:11 - 5:14
    default blue colour we see in a new empty scene.
  • 5:15 - 5:17
    Clear Flags determines what the background
  • 5:17 - 5:19
    will be for a camera. This setting is particularly
  • 5:19 - 5:21
    important when using multiple cameras.
  • 5:22 - 5:24
    Each camera stores colour and depth information
  • 5:24 - 5:26
    when it renders it's view. The portions of the
  • 5:26 - 5:29
    screen that are not drawn upon are considered empty.
  • 5:29 - 5:31
    The Clear Flags property will determine
  • 5:31 - 5:33
    what is shown in this empty space.
  • 5:33 - 5:35
    If we have a skybox set in our render
  • 5:35 - 5:38
    settings the background will be a skybox.
  • 5:38 - 5:41
    Skybox is the default clear flag for any camera.
  • 5:42 - 5:44
    A skybox is a material that contains
  • 5:44 - 5:46
    several images that surround the entire scene
  • 5:46 - 5:48
    providing a textured background for that scene.
  • 5:48 - 5:50
    For more information on skyboxes and
  • 5:50 - 5:52
    render settings see the appropriate lessons.
  • 5:53 - 5:55
    If we don't have a skybox set, or we choose
  • 5:55 - 5:57
    solid colour as our clear flag.
  • 5:57 - 5:59
    The colour value from the background property
  • 5:59 - 6:01
    will be used behind any of our objects
  • 6:01 - 6:02
    in the scene.
  • 6:03 - 6:05
    Depth Only is primarily used for
  • 6:05 - 6:07
    multiple cameras. We will cover depth only
  • 6:07 - 6:08
    in a moment.
  • 6:09 - 6:11
    Don't Clear will result in each frame
  • 6:11 - 6:13
    being drawn over the last, creating
  • 6:13 - 6:15
    a smear effect. This setting isn't typically
  • 6:15 - 6:16
    used in games.
  • 6:19 - 6:21
    When using multiple cameras the most practical
  • 6:21 - 6:24
    setting for clear flags is depth only.
  • 6:25 - 6:27
    With this setting each camera is given a
  • 6:27 - 6:29
    value and depth and the contents of each
  • 6:29 - 6:31
    camera's view are layered on top of each other
  • 6:31 - 6:33
    in depth order, starting with
  • 6:33 - 6:34
    lowest depth first.
  • 6:35 - 6:37
    Normally the main camera is assigned
  • 6:37 - 6:39
    the lowest depth value
  • 6:39 - 6:41
    and has it's clear flag set to either
  • 6:41 - 6:43
    skybox or solid colour.
  • 6:45 - 6:47
    All of the other cameras have their clear
  • 6:47 - 6:50
    flags set to depth only. This way there is
  • 6:50 - 6:52
    one ultimate background, and the images
  • 6:52 - 6:54
    of all the other cameras are
  • 6:54 - 6:56
    layered on top of the main camera.
  • 6:57 - 6:59
    The content of what the camera is rendering
  • 6:59 - 7:01
    is limited by the Culling Mask property.
  • 7:01 - 7:03
    The Culling Mask drop down will list
  • 7:03 - 7:05
    all the layers available in the scene.
  • 7:05 - 7:07
    The camera will render only those objects
  • 7:07 - 7:10
    on the layers selected in it's culling mask.
  • 7:10 - 7:12
    For more information on layers and how to
  • 7:12 - 7:14
    use then see the appropriate lesson.
  • 7:15 - 7:17
    In the case of the User Interface overlay
  • 7:17 - 7:21
    we have the interface element set to the
  • 7:21 - 7:23
    UI layer. Our UI camera has it's culling mask
  • 7:23 - 7:26
    set to render only the objects on the User Interface layer
  • 7:27 - 7:30
    We have our clear flag set to depth only
  • 7:30 - 7:32
    and the depth set to the highest value
  • 7:32 - 7:34
    of all the cameras in the scene.
  • 7:34 - 7:36
    This way the UI camera only draws
  • 7:36 - 7:38
    the UI element based on the culling mask
  • 7:38 - 7:41
    setting and the UI element draws on top
  • 7:41 - 7:44
    of all of the other layers, based on the depth.
  • 7:44 - 7:46
    It is also worth noting that the camera is set to
  • 7:46 - 7:48
    orthographic to remove any possible
  • 7:48 - 7:50
    perspective on the UI element.
  • 7:51 - 7:53
    Typical uses of multiple cameras
  • 7:53 - 7:55
    are to render UI elements like
  • 7:55 - 7:57
    mini maps or heads-up displays over the
  • 7:57 - 8:00
    world view, make rear-view mirrors and
  • 8:00 - 8:02
    missile cameras, or to force the drawing order
  • 8:02 - 8:04
    of objects in the scene, like making
  • 8:04 - 8:06
    sure that a gun in a first person shooter
  • 8:06 - 8:09
    doesn't get drawn inside the level geometry.
  • 8:09 - 8:12
    The normalised viewport rect, render path,
  • 8:12 - 8:14
    target texture and HDR properties
  • 8:14 - 8:17
    are more advanced and will be covered in an other lesson.
Title:
Cameras - Unity Official Tutorials
Description:

more » « less
Duration:
08:19

English subtitles

Revisions