< Return to Video

Beginner 2D UFO Game 6 of 9: Creating Collectable Objects - Unity Official Tutorials

  • 0:02 - 0:06
    So far we have created a play field
  • 0:06 - 0:08
    and a Player game object
  • 0:08 - 0:10
    and we have the player moving
  • 0:10 - 0:12
    under our control.
  • 0:12 - 0:14
    In this lesson we'll create
  • 0:14 - 0:16
    the collectable objects
  • 0:16 - 0:18
    for our player to pick up.
  • 0:19 - 0:21
    First open the Sprites folder.
  • 0:22 - 0:24
    Drag the sprite Pickup
  • 0:24 - 0:26
    in to the hierarchy.
  • 0:30 - 0:32
    You'll notice after dragging in
  • 0:32 - 0:34
    the Pickup sprite that we
  • 0:34 - 0:36
    still can't see it.
  • 0:37 - 0:39
    That's because it's being rendered
  • 0:39 - 0:42
    behind both the Player
  • 0:42 - 0:44
    and Background sprites.
  • 0:44 - 0:46
    To bring it forward we
  • 0:46 - 0:49
    need to set the sorting layer
  • 0:49 - 0:51
    in the sprite renderer component.
  • 0:53 - 0:56
    Set the sorting layer to Pickups.
  • 0:59 - 1:01
    Now the Player game object
  • 1:01 - 1:03
    is still in the way.
  • 1:04 - 1:06
    This is because the Player
  • 1:06 - 1:08
    sorting layer is in front
  • 1:08 - 1:10
    of the Pickups sorting layer.
  • 1:11 - 1:14
    Let's temporarily deactivate
  • 1:14 - 1:16
    the Player game object.
  • 1:16 - 1:18
    Select the Player game object
  • 1:18 - 1:20
    and deselect the checkbox
  • 1:20 - 1:22
    in front of the Name field.
  • 1:23 - 1:26
    This is the game object's Active checkbox.
  • 1:26 - 1:28
    Deselecting it will deactivate
  • 1:28 - 1:31
    the game object in the scene.
  • 1:31 - 1:33
    This will give us a clear working space
  • 1:33 - 1:35
    for our new pickup object
  • 1:35 - 1:36
    which we can now see.
  • 1:37 - 1:39
    Focus the scene view camera on the
  • 1:39 - 1:41
    pickup object by highlighting it
  • 1:41 - 1:43
    and pressing the F key whilst
  • 1:43 - 1:45
    the cursor is over the scene view.
  • 1:46 - 1:48
    So that we can collide with the pickup
  • 1:48 - 1:51
    let's add a circle collider 2D
  • 1:51 - 1:53
    component to it.
  • 1:53 - 1:55
    With the pickup highlighted click
  • 1:55 - 1:58
    Add Component - Physics 2D
  • 1:58 - 2:01
    then Circle Collider 2D.
  • 2:02 - 2:04
    Adjust the Radius property
  • 2:04 - 2:06
    of the circle collider 2D
  • 2:06 - 2:09
    so that it fits the sprite artwork visually b
  • 2:10 - 2:12
    by dragging in the Radius field.
  • 2:13 - 2:15
    To be effective the collectable
  • 2:15 - 2:18
    must attract the attention of the player.
  • 2:19 - 2:21
    So let's make the object more attractive.
  • 2:22 - 2:24
    There's one thing I feel certainly
  • 2:24 - 2:26
    attracts the attention of a player
  • 2:26 - 2:28
    and that is movement.
  • 2:28 - 2:30
    So let's rotate our pickup.
  • 2:30 - 2:32
    One way to do this is with a script.
  • 2:33 - 2:36
    With the pickup object still selected
  • 2:36 - 2:38
    use the Add Component button
  • 2:38 - 2:40
    in the inspector.
  • 2:40 - 2:43
    Let's create a new script called Rotator.
  • 2:44 - 2:46
    Click Create and Add to confirm
  • 2:46 - 2:48
    and let's organise the script by
  • 2:48 - 2:50
    placing it in the Scripts folder.
  • 2:52 - 2:54
    Open the Scripts folder and
  • 2:54 - 2:56
    double click to open it for editing.
  • 2:59 - 3:01
    We want the sprite to spin and we want
  • 3:01 - 3:03
    to do it with this script.
  • 3:03 - 3:05
    Let's remove the sample code
  • 3:05 - 3:07
    we don't need.
  • 3:08 - 3:10
    We will not be using forces
  • 3:10 - 3:12
    so we can use Update
  • 3:12 - 3:14
    rather than Fixed Update.
  • 3:14 - 3:17
    We want to rotate the object every frame.
  • 3:17 - 3:19
    To make the pickups spin we
  • 3:19 - 3:22
    don't want to set the transform rotation
  • 3:22 - 3:25
    but we want to rotate the transform.
  • 3:26 - 3:29
    Type Transform inside Update.
  • 3:32 - 3:34
    Select it and hold down the
  • 3:34 - 3:36
    control key on windows or the command
  • 3:36 - 3:39
    key on mac and type '
  • 3:41 - 3:43
    Again this brings up the page
  • 3:43 - 3:44
    with a search term Transform.
  • 3:45 - 3:47
    Select Transform.
  • 3:49 - 3:53
    Scroll down until you see Transform's public functions.
  • 3:55 - 3:58
    There are two main ways to affect the transform.
  • 3:58 - 4:00
    These are Translate and Rotate.
  • 4:01 - 4:03
    Translate moves the game object
  • 4:03 - 4:05
    using it's transform.
  • 4:06 - 4:09
    Rotate rotates the game object
  • 4:09 - 4:11
    using it's transform.
  • 4:12 - 4:14
    We will use rotate.
  • 4:14 - 4:16
    So let's click on the link.
  • 4:16 - 4:20
    This brings up the page for Transform.Rotate.
  • 4:21 - 4:23
    Note again the two signatures.
  • 4:24 - 4:27
    One is using a vector3
  • 4:27 - 4:29
    and the other is using 3
  • 4:29 - 4:34
    float values for X, Y or Z.
  • 4:35 - 4:37
    Both have the optional parameter
  • 4:37 - 4:39
    Space which we will
  • 4:39 - 4:41
    leave at default for this lesson.
  • 4:41 - 4:43
    Again we will choose the most simple
  • 4:43 - 4:47
    form that only uses the vector3 for direction.
  • 4:47 - 4:49
    Let's return to our code.
  • 4:52 - 4:54
    After transform,
  • 4:54 - 4:56
    making sure that transform is written to
  • 4:56 - 4:57
    begin with a lowercase t,
  • 4:58 - 5:06
    write .rotate (new Vector3 (0, 0, 45))
  • 5:08 - 5:10
    This means we'll rotate
  • 5:10 - 5:12
    around the Z axis.
  • 5:13 - 5:15
    Now this action also needs
  • 5:15 - 5:18
    to be smooth and frame rate independent
  • 5:18 - 5:21
    so we need to multiply the vector3 value
  • 5:21 - 5:24
    by Time.deltaTime.
  • 5:25 - 5:27
    It is worth noting that even
  • 5:27 - 5:29
    though we are working with a 2D
  • 5:29 - 5:32
    sprite we are using a vector3
  • 5:32 - 5:35
    to rotate the collectable's transform.
  • 5:35 - 5:37
    This is because the transform
  • 5:37 - 5:39
    of the 2D sprite
  • 5:39 - 5:42
    still exists in the 3D volume.
  • 5:43 - 5:45
    When using the transform component
  • 5:45 - 5:47
    with 2D objects we simply
  • 5:47 - 5:50
    ignore the axis we don't need.
  • 5:51 - 5:53
    Now that we are done with our Rotator script
  • 5:53 - 5:56
    save this script and return to Unity.
  • 5:57 - 5:59
    Let's test by entering play mode.
  • 6:00 - 6:03
    And we can see our pickup object rotates.
  • 6:05 - 6:07
    Let's exit play mode.
  • 6:08 - 6:11
    Okay, we have the start of a working
  • 6:11 - 6:12
    pickup object.
  • 6:12 - 6:14
    Next we want to place these pickups
  • 6:14 - 6:16
    around the game area.
  • 6:17 - 6:19
    But before we do this we need to do
  • 6:19 - 6:20
    one important step.
  • 6:21 - 6:23
    We need to make our pickup object
  • 6:23 - 6:24
    in to a prefab.
  • 6:25 - 6:27
    Remember, a prefab is an
  • 6:27 - 6:29
    asset that contains a template
  • 6:29 - 6:32
    or blueprint of a game object
  • 6:32 - 6:34
    or game object family.
  • 6:34 - 6:36
    We create a prefab from an
  • 6:36 - 6:40
    existing game object or game object family
  • 6:40 - 6:42
    and once we create it
  • 6:42 - 6:44
    we can use it in any scene
  • 6:44 - 6:46
    in our current project.
  • 6:47 - 6:49
    With a prefab of our pickup object
  • 6:49 - 6:51
    we will be able to make changes to
  • 6:51 - 6:53
    a single instance in our scene
  • 6:53 - 6:55
    or to the prefab asset itself.
  • 6:56 - 6:58
    And all of the pickup objects in our
  • 6:58 - 7:01
    game will be updated with those changes.
  • 7:02 - 7:04
    You'll notice we already have a folder
  • 7:04 - 7:05
    to hold our prefabs.
  • 7:06 - 7:08
    Let's drag the Pickup game object
  • 7:08 - 7:10
    from our hierarchy and place
  • 7:10 - 7:12
    it in to our Prefabs folder.
  • 7:13 - 7:15
    When we drag and item from our hierarchy
  • 7:15 - 7:17
    in to our project view
  • 7:17 - 7:19
    we create a new prefab
  • 7:19 - 7:21
    asset in our project.
  • 7:23 - 7:25
    Before we spread our collectables
  • 7:25 - 7:27
    around the game area we should create
  • 7:27 - 7:29
    a new game object
  • 7:29 - 7:31
    to hold our pickups and
  • 7:31 - 7:33
    to help organise our hierarchy.
  • 7:33 - 7:35
    Let's create a new game object.
  • 7:37 - 7:39
    And call it Pickups.
  • 7:41 - 7:43
    Check the transform to make sure our
  • 7:43 - 7:45
    Pickups holder object
  • 7:45 - 7:48
    is at origin, or (0, 0, 0).
  • 7:49 - 7:51
    And if not use the component
  • 7:51 - 7:53
    context menu to reset it.
  • 7:53 - 7:57
    Next drag our Pickup game object on to it.
  • 7:58 - 8:00
    Now we want to spread a
  • 8:00 - 8:02
    number of these pickup objects
  • 8:02 - 8:04
    around the play area.
  • 8:04 - 8:06
    Make sure the pickup game object is
  • 8:06 - 8:08
    selected, and not the parent.
  • 8:09 - 8:11
    Now let's back out a little
  • 8:11 - 8:13
    so we can see the entire game area.
  • 8:15 - 8:17
    Click and drag the pickup game object
  • 8:17 - 8:19
    to move it in to the
  • 8:19 - 8:21
    first pickup position.
  • 8:21 - 8:24
    I'm going to place mine at the top of the playing area.
  • 8:25 - 8:27
    With the game object still selected
  • 8:27 - 8:29
    duplicate it.
  • 8:29 - 8:31
    This can be done either by selecting
  • 8:31 - 8:35
    Edit - Duplicate, or by using the hot key combination.
  • 8:35 - 8:37
    This is command-D on mac,
  • 8:37 - 8:39
    or control-D on windows.
  • 8:41 - 8:43
    Now let's position the
  • 8:43 - 8:45
    second instance of the prefab.
  • 8:48 - 8:50
    Using the hot keys we will
  • 8:50 - 8:52
    create a few more, placing them
  • 8:52 - 8:54
    around the play area.
  • 9:08 - 9:10
    Okay, I've created 12.
  • 9:11 - 9:13
    Let's hit play and test.
  • 9:15 - 9:17
    Excellent, these pickup prefabs
  • 9:17 - 9:18
    are working great.
  • 9:18 - 9:20
    In the next assignment we'll learn how to
  • 9:20 - 9:23
    pick them up and to count them.
Title:
Beginner 2D UFO Game 6 of 9: Creating Collectable Objects - Unity Official Tutorials
Description:

more » « less
Video Language:
English
Duration:
09:27

English subtitles

Revisions