< Return to Video

Adding a background - 04 - Space Shooter - Unity Official Tutorials

  • 0:02 - 0:07
    We're making a space game and space is, well, black.
  • 0:07 - 0:09
    But flat black is boring.
  • 0:10 - 0:13
    So let's add a background to our game.
  • 0:13 - 0:15
    First, let's begin by deactivating our player
  • 0:15 - 0:19
    game object, it will simply be in the way for now.
  • 0:20 - 0:22
    Next create a quad to hold our
  • 0:22 - 0:27
    background image by selecting Create - Quad
  • 0:27 - 0:29
    from the Hierarchy's Create menu.
  • 0:30 - 0:32
    Rename this Background.
  • 0:33 - 0:34
    Where is it?
  • 0:34 - 0:38
    We've created a quad, but we can't see it in our game.
  • 0:38 - 0:41
    Let's switch to the Scene view to see what's going on.
  • 0:41 - 0:44
    There it is, it's oriented in a way
  • 0:44 - 0:46
    that's not facing the main camera.
  • 0:46 - 0:48
    The main camera can't see it.
  • 0:49 - 0:51
    To adjust this let's first reset
  • 0:51 - 0:53
    the game object's transform to make sure
  • 0:53 - 0:55
    it is at origin
  • 0:55 - 0:57
    and then let's change the background's
  • 0:57 - 1:00
    transform rotation along the X axis.
  • 1:01 - 1:03
    Let's set this value to 90 degrees.
  • 1:04 - 1:06
    Now the background quad is facing
  • 1:06 - 1:08
    towards our main camera.
  • 1:08 - 1:10
    and we can see it now in the game view.
  • 1:11 - 1:14
    I'm going to switch back to the Scene view.
  • 1:14 - 1:16
    The stock quad comes with a
  • 1:16 - 1:18
    component we don't need.
  • 1:18 - 1:20
    We can safely remove the mesh collider
  • 1:20 - 1:23
    component as we won't be using it.
  • 1:24 - 1:27
    Next, let's add a texture to this background.
  • 1:28 - 1:32
    In Assets there is a directory called Textures.
  • 1:32 - 1:35
    Inside Textures we will find an image
  • 1:35 - 1:37
    tile_nebula_green.
  • 1:37 - 1:39
    Let's select this image.
  • 1:39 - 1:41
    When we select an image and inspect it
  • 1:41 - 1:44
    we are looking at that image's importer
  • 1:45 - 1:47
    We will leave the importer settings as they are.
  • 1:48 - 1:50
    But if the preview window is open
  • 1:50 - 1:53
    we can some additional information about this image.
  • 1:53 - 1:55
    If the preview window is collapsed
  • 1:55 - 1:58
    we can drag the bar titled Preview upwards
  • 1:58 - 2:00
    until we see the image clearly.
  • 2:01 - 2:04
    The information at the bottom of the Preview window
  • 2:04 - 2:06
    tells us the current resolution of the
  • 2:06 - 2:08
    image in our project, the current compression method,
  • 2:08 - 2:12
    if any, and the total size of the imported image.
  • 2:12 - 2:14
    What is important for us to note here is that
  • 2:14 - 2:16
    this is a very large image.
  • 2:17 - 2:19
    1024 by 2048.
  • 2:20 - 2:22
    So this image is more than large enough to stretch
  • 2:22 - 2:24
    behind our 600 by 900 game view.
  • 2:25 - 2:27
    This is also telling us that the image is not square
  • 2:27 - 2:29
    but rectangular.
  • 2:30 - 2:32
    There are a number of ways that we can get
  • 2:32 - 2:34
    a texture on to a mesh.
  • 2:34 - 2:36
    Let's try one of the most simply ways.
  • 2:37 - 2:39
    Grab the image, drag it,
  • 2:40 - 2:42
    and drop it on the background quad in the scene.
  • 2:43 - 2:44
    Done!
  • 2:44 - 2:47
    Let's get a better view of our background.
  • 2:47 - 2:49
    Use Frame Selected to focus the
  • 2:49 - 2:51
    Scene view camera
  • 2:51 - 2:54
    The image has been applied to the background quad.
  • 2:54 - 2:56
    It's dark and squashed but it's there.
  • 2:58 - 3:00
    What just happened?
  • 3:01 - 3:03
    We can see that the texture has been attached
  • 3:03 - 3:06
    to a material and the material has been
  • 3:06 - 3:08
    associated with the quad by a reference
  • 3:08 - 3:10
    in the mesh renderer.
  • 3:12 - 3:15
    Our mesh filter holds the mesh data,
  • 3:15 - 3:17
    in this case the quad.
  • 3:18 - 3:20
    The mesh renderer renders that mesh using
  • 3:20 - 3:24
    the materials in the mesh renderer's Materials Array.
  • 3:26 - 3:29
    The renderer is only able to use a texture
  • 3:29 - 3:31
    if it's part of a material.
  • 3:31 - 3:33
    We did not create a material, we simply
  • 3:33 - 3:35
    dragged the texture on to the quad.
  • 3:36 - 3:40
    It was Unity that created the material for us.
  • 3:40 - 3:42
    When we drag a texture on to a mesh,
  • 3:42 - 3:44
    if Unity cannot find an associated material
  • 3:44 - 3:46
    with that texture Unity will create
  • 3:46 - 3:48
    a new material for us.
  • 3:49 - 3:52
    Then Unity will automatically add that material
  • 3:52 - 3:54
    to that mesh renderer's Materials Array.
  • 3:55 - 3:58
    This automatic material will always
  • 3:58 - 4:00
    with the default diffuse shader.
  • 4:02 - 4:04
    Let's look at our background.
  • 4:04 - 4:07
    Is it ready to go? No.
  • 4:07 - 4:09
    It's small, square, squashed,
  • 4:09 - 4:12
    a bit dark and a little hard to see.
  • 4:13 - 4:15
    The first thing we need to do is scale up
  • 4:15 - 4:17
    our quad to fit our background.
  • 4:18 - 4:19
    We can do this by changing the
  • 4:19 - 4:21
    transform's Scale properties.
  • 4:22 - 4:25
    The quad is a very simple 2 dimensional plane.
  • 4:25 - 4:28
    We can scale the quad along the X and Y axis,
  • 4:28 - 4:30
    but not along the Z axis.
  • 4:30 - 4:33
    Trying to scale the Z axis does nothing.
  • 4:34 - 4:37
    Let's reset the scale now that we've played with it.
  • 4:38 - 4:40
    What scale should we use?
  • 4:40 - 4:42
    Let's first look at the texture itself.
  • 4:43 - 4:46
    It's 1024 by 2048.
  • 4:46 - 4:49
    To keep the image undistorted when we scale it up
  • 4:49 - 4:53
    we should keep an aspect ratio of 1:2.
  • 4:53 - 4:56
    This means our X scale should always be
  • 4:56 - 4:58
    half of our Y scale.
  • 4:58 - 5:00
    Or conversely our Y scale must always
  • 5:00 - 5:01
    be twice our X.
  • 5:02 - 5:04
    To see this properly in the game let's
  • 5:04 - 5:06
    switch back to the Game view.
  • 5:07 - 5:09
    In the Game view select the background quad
  • 5:09 - 5:11
    and scale it along the X axis
  • 5:11 - 5:13
    until it fills the screen.
  • 5:14 - 5:16
    It's more than filling the screen when we use
  • 5:16 - 5:18
    a scale value of 12 or more.
  • 5:19 - 5:21
    We have enough resolution that we can afford
  • 5:21 - 5:24
    to lose some of the image off the sides.
  • 5:24 - 5:26
    So let's pick a good round number,
  • 5:26 - 5:29
    like 15. It's more than big enough.
  • 5:29 - 5:33
    So that we can maintain our 1:2 aspect ratio
  • 5:33 - 5:35
    what is 15 times 2? 30.
  • 5:36 - 5:39
    If we have 15 in our X we need 30 in our Y.
  • 5:40 - 5:42
    Now the background fills the game view
  • 5:42 - 5:44
    with no distortion.
  • 5:44 - 5:46
    Let's look at it in the Scene view.
  • 5:46 - 5:48
    Use Frame Selected to focus the
  • 5:48 - 5:51
    Scene view camera to the background's new size.
  • 5:51 - 5:55
    and we can see that we now have a nice big background.
  • 5:55 - 5:57
    The background is still a bit too dark
  • 5:58 - 6:02
    The material is using a simple diffuse shader.
  • 6:02 - 6:04
    This means that the texture is being treated like
  • 6:04 - 6:07
    a simple painted surface using matt,
  • 6:07 - 6:09
    not glossy paint.
  • 6:09 - 6:12
    The surface of our background is being lit
  • 6:12 - 6:13
    by the lights in our scene.
  • 6:14 - 6:17
    These lights have been setup to light our player ship.
  • 6:18 - 6:20
    1 light, the rim light, is shining up
  • 6:20 - 6:22
    from below and won't light the surface
  • 6:22 - 6:24
    of the background at all.
  • 6:25 - 6:27
    The other 2 have very shallow angles
  • 6:27 - 6:30
    that only rake the surface of the quad.
  • 6:30 - 6:32
    So our background isn't receiving very
  • 6:32 - 6:34
    much light from our lighting rig.
  • 6:34 - 6:36
    This is great for our player ship
  • 6:36 - 6:39
    but not so great for our background.
  • 6:40 - 6:42
    We could add a light shining straight
  • 6:42 - 6:44
    down to light the background.
  • 6:44 - 6:46
    But this would add another light to the ship
  • 6:46 - 6:49
    lighting it in a way that we wouldn't want.
  • 6:49 - 6:51
    We could put the background and it's new light
  • 6:51 - 6:53
    on a separate layer so that the background
  • 6:53 - 6:55
    light did not mix with out existing lights
  • 6:55 - 6:57
    on the player ship.
  • 6:57 - 6:59
    But there is a better way.
  • 7:00 - 7:03
    For more information on layers and how to use them
  • 7:03 - 7:05
    see the information linked below.
  • 7:07 - 7:09
    If we think about our background image
  • 7:09 - 7:11
    we simply want to have it display
  • 7:11 - 7:13
    behind our game.
  • 7:13 - 7:15
    It's a flat plane, we do not need specialised
  • 7:15 - 7:17
    lighting like we need for our ship
  • 7:18 - 7:20
    and it's a waste to shine a directional light
  • 7:20 - 7:24
    on a simply quad just to flat light it's contents.
  • 7:25 - 7:27
    When dealing with a material,
  • 7:27 - 7:29
    how that material treats the texture
  • 7:29 - 7:32
    depends upon what shader we choose.
  • 7:32 - 7:36
    Diffuse treats the texture like matte paint.
  • 7:37 - 7:40
    The specular shader will treat that texture
  • 7:40 - 7:43
    like glossy paint, or shiny plastic.
  • 7:44 - 7:45
    There are many other built-in shaders
  • 7:45 - 7:47
    that come with Unity.
  • 7:47 - 7:49
    More can be found on the Asset Store
  • 7:49 - 7:51
    and we can write our own custom shaders.
  • 7:52 - 7:54
    For more information on shaders
  • 7:54 - 7:56
    see the information linked below.
  • 7:57 - 7:59
    For our purposes we don't need any
  • 7:59 - 8:01
    specialised lighting on our background.
  • 8:01 - 8:05
    As a matter of fact we don't need any lighting at all.
  • 8:06 - 8:09
    For our background, let's change the shader.
  • 8:09 - 8:12
    Let's choose Unlit - Texture
  • 8:12 - 8:14
    for the shader on the nebula material.
  • 8:15 - 8:17
    Now our background is independent
  • 8:17 - 8:20
    of our lighting system, and it displays
  • 8:20 - 8:22
    the texture exactly as it looks
  • 8:22 - 8:24
    in the original image
  • 8:24 - 8:26
    and it uses no lights at all.
  • 8:26 - 8:29
    In the game it's nice and bright.
  • 8:29 - 8:32
    If we change the shader back to diffuse
  • 8:32 - 8:34
    we can see how much darker it is in the game.
  • 8:38 - 8:40
    Let's bring back our player and check that
  • 8:40 - 8:42
    everything looks good in context
  • 8:42 - 8:44
    now that we have a background in place.
  • 8:45 - 8:47
    Okay, something's wrong with our ship.
  • 8:48 - 8:49
    Let's look at it in the Scene view
  • 8:49 - 8:51
    to see what's going on.
  • 8:52 - 8:54
    Ah, the player ship is buried
  • 8:54 - 8:56
    in the middle of the background,
  • 8:56 - 8:58
    they are both at origin.
  • 8:58 - 9:01
    The background should be behind the game.
  • 9:01 - 9:03
    So let's grab the background and adjust it.
  • 9:03 - 9:05
    along the Y axis.
  • 9:05 - 9:08
    We can slide it in to a good position by hand
  • 9:08 - 9:11
    or we can simply set the transform position
  • 9:11 - 9:13
    on the Y axis to -10.
  • 9:13 - 9:17
    Now the background is down and out of the way.
  • 9:19 - 9:21
    When we look back at the game we can't
  • 9:21 - 9:23
    see any change to the background,
  • 9:23 - 9:24
    except for the fact that it is no longer
  • 9:24 - 9:26
    in the middle of our space ship.
  • 9:26 - 9:28
    This is due to the fact that our camera
  • 9:28 - 9:32
    is orthographic and has no perspective.
  • 9:32 - 9:34
    The background can be any distance from the
  • 9:34 - 9:36
    camera as long as it's within it's clipping plane
  • 9:36 - 9:38
    and still look exactly the same.
  • 9:40 - 9:43
    We have now setup our background and our lighting.
  • 9:43 - 9:47
    Next let's begin to create game play elements
  • 9:47 - 9:49
    by moving the player ship.
Title:
Adding a background - 04 - Space Shooter - Unity Official Tutorials
Description:

more » « less
Video Language:
Russian
Duration:
09:50

English subtitles

Revisions