Return to Video

(Unity 4) Moving the camera - 03 - Roll-a-ball - Unity Official Tutorials

  • 0:01 - 0:04
    The camera doesn't move and from it's current position
  • 0:04 - 0:05
    cannot see very much.
  • 0:05 - 0:08
    We need to tie the camera to the player game object.
  • 0:09 - 0:11
    First let's set the position of the camera.
  • 0:11 - 0:14
    Let's lift it up by 10 units and tilt it
  • 0:14 - 0:16
    down by about 45 degrees.
  • 0:17 - 0:19
    Next let's make the camera a child of the
  • 0:19 - 0:21
    player game object.
  • 0:21 - 0:24
    This is a typical third-person setup,
  • 0:24 - 0:26
    with the camera as a child of the player
  • 0:26 - 0:28
    when we move the player's position
  • 0:28 - 0:30
    the camera moves with it.
  • 0:31 - 0:35
    When the player rotates the camera rotates as well.
  • 0:36 - 0:38
    Let's look at this from a position where we
  • 0:38 - 0:42
    can see both the player and the camera game object
  • 0:43 - 0:45
    Move the player,
  • 0:47 - 0:48
    rotate the player,
  • 0:48 - 0:50
    the child camera moves with it.
  • 0:51 - 0:54
    Now let's reset the player and test.
  • 0:55 - 0:59
    We enter play move, hold down the up arrow to move,
  • 0:59 - 1:01
    Whoa what's happening here?
  • 1:01 - 1:05
    Okay, well as the camera is a child of the
  • 1:05 - 1:07
    player's sphere, even thought the camera is not
  • 1:07 - 1:11
    moving at all relative to the player's game object,
  • 1:11 - 1:13
    the player game object is rotating like crazy
  • 1:13 - 1:16
    so the camera's point of view rotates with it.
  • 1:16 - 1:17
    Let's exit play mode.
  • 1:18 - 1:20
    Unlike a normal third-person game
  • 1:20 - 1:24
    our player game object is rotating on all 3 axis
  • 1:24 - 1:26
    not just 1.
  • 1:26 - 1:28
    In a typical third-person setup
  • 1:28 - 1:31
    the camera as a child of the player game object
  • 1:31 - 1:33
    will always be in a position relative
  • 1:33 - 1:34
    to it's immediate parent
  • 1:34 - 1:36
    and this position will be the parent's
  • 1:36 - 1:40
    position in the game, modified or offset by
  • 1:40 - 1:42
    any values in the child's transform.
  • 1:42 - 1:44
    We can't have the camera as a child of
  • 1:44 - 1:46
    the player, so let's detach it.
  • 1:48 - 1:50
    Our offset value will be the difference
  • 1:50 - 1:52
    between the player game object and the camera.
  • 1:53 - 1:56
    As the player game object is just above origin
  • 1:56 - 1:58
    or just above (0, 0, 0).
  • 1:58 - 2:00
    For simplicity in this assignment we will use
  • 2:00 - 2:03
    the camera's transform position value
  • 2:03 - 2:05
    as the offset value.
  • 2:05 - 2:07
    This is because the camera's position is based on
  • 2:07 - 2:09
    the distance from origin, which is very close
  • 2:09 - 2:11
    to that of the player game object.
  • 2:12 - 2:14
    Now we need to associate the camera with
  • 2:14 - 2:16
    the player game object, not as a child
  • 2:16 - 2:18
    but with a script.
  • 2:18 - 2:22
    Using the Add Component button choose New Script.
  • 2:24 - 2:26
    We are writing in C#
  • 2:27 - 2:30
    and name the script CameraController
  • 2:30 - 2:33
    and then click on Create and Add
  • 2:33 - 2:35
    or simply hit the return or enter key
  • 2:35 - 2:37
    to confirm our selection.
  • 2:37 - 2:39
    We should note, this way of creating a script
  • 2:39 - 2:41
    will create that script asset on
  • 2:41 - 2:44
    the root or top level of our project view.
  • 2:46 - 2:49
    File CameraController in the scripts folder
  • 2:49 - 2:51
    and open it for editing.
  • 2:53 - 2:55
    We need 2 variables here.
  • 2:56 - 2:59
    A public game object reference to the player
  • 3:00 - 3:05
    and a private vector3 to hold our offset value.
  • 3:05 - 3:07
    Offset is private because we can
  • 3:07 - 3:10
    set that value here in the script.
  • 3:10 - 3:12
    For our offset value we will use the
  • 3:12 - 3:15
    camera's current transform position.
  • 3:15 - 3:20
    So in start we can make offset equal transform position.
  • 3:21 - 3:24
    And then every frame we set our
  • 3:24 - 3:28
    transform position to the player's transform position
  • 3:28 - 3:30
    pus the offset.
  • 3:32 - 3:36
    However, update is not the best place for this code.
  • 3:36 - 3:38
    For follow cameras, procedural animation,
  • 3:38 - 3:41
    and gathering last known states
  • 3:41 - 3:43
    it's best to use LateUpdate.
  • 3:44 - 3:46
    So let's test this. Let's save our script
  • 3:46 - 3:48
    and return to Unity.
  • 3:49 - 3:52
    First we need to create a reference to the
  • 3:52 - 3:54
    player game object by dragging the player
  • 3:54 - 3:57
    game object in to the Player slot
  • 3:57 - 3:59
    in the camera controller's component.
  • 4:00 - 4:02
    Enter play mode
  • 4:04 - 4:06
    and now we get the behaviour we want.
  • 4:08 - 4:10
    The camera follows the rolling ball
  • 4:10 - 4:12
    without rotating
  • 4:13 - 4:15
    even as the ball goes over the edge.
  • 4:18 - 4:20
    In the next assignment we will setup the basic
  • 4:20 - 4:22
    play area and create and place our
  • 4:22 - 4:24
    special pick-up objects.
Title:
(Unity 4) Moving the camera - 03 - Roll-a-ball - Unity Official Tutorials
Description:

more » « less
Duration:
04:26

English subtitles

Revisions