Becoming familiar with Unity: Part 6

b10

In this part of the tutorial we learn how to navigate through the scene by adding controllers (first- and third-person controllers).

Adding a first person controller

At this stage, we have a relatively basic scene with a few colored and textured boxes, a light, and a camera. We will, in the next section, import a character controller that we will use to navigate through the scene. These built-in elements include all necessary features to simulate the movement of a character in a three-dimensional environment (e.g., walk, run, jump, look around, etc.)

  • Select Assets | Import Package: you will see a list of assets that can be imported in your project. These assets are not imported by default, and you can pick and choose which one you will import based on your needs. For now, we will choose the asset called Characters.
  • Select Characters from the list (or Character Controllers if you are using an earlier version of Unity).
  • After a few seconds, a new window labeled Importing Packages will appear.

import_controllers

Figure 47: Importing character controllers

If you scroll down, you may notice that this package includes a combination of objects, sounds, and scripts. You may also notice tick boxes to the left of these objects. This is indicates that you can import selectively. In other words, you can decide which element you wish to import in your project.

For now, as we will import the full package, we will leave these ticked and click on the button Import.

  • After a few seconds, the import should be complete.
  • These assets are usually saved in the folder labeled Standard Assets > Characters (or Standard Assets > Character Controllers if you are using a previous version of Unity).
  • If you open this folder, we can notice that it includes assets for a both First-Person Controller (view from the eyes of the main character) and Third-Person Controller (view from the camera located behind the main character).

Now that we have imported the relevant package, let’s use one of the character controllers to navigate through our scene:

If you are using Unity 5: locate the folder Assets > Standard Assets > Characters > First PersonCharacter > Prefabs and drag and drop the icon labeled FPSController on to the scene.

addingFPC_unity4

Figure 48: Adding the First-Person Controller in Unity 5

If you are using a previous version of Unity (e.g., 4.5): locate the folder Assets > Standard Assets > Character Controllers and drag and drop the icon labeled First-Person Controller to the scene.

addingFPC_unity4x

Figure 49: Adding a First person Controller in Unity 4.x

  • Once this is done we can move the First-Person controller above the ground in the scene view and check how the game looks like in the game view.
  • We will also deactivate the main camera, as we don’t need it anymore because the FPS controller includes its own camera. To do so we just need to select the object labeled Main Camera in the hierarchy, and, using the inspector, deselect the box to the left of the label Main Camera.
  • After making these modifications, it is time to test the scene by pressing the Play button  (the left-most triangle located at the top of the window) or alternatively pressing the shortcut CTRL+P.
  • As we play the scene, we can navigate using the arrow keys from the keyboard, we can jump (using the space key), look around using the mouse, or run by pressing the arrow key while also pressing the SHIFT key.

navigating_scene_FPC

Figure 50: Navigating through the scene

Note that you can switch between game and scene views, as the game is playing, to look at the attributes of all objects in the scene. We could also, for example display both views simultaneously by dragging the game view in the lower part of the screen, as illustrated on the next figure.

displaying_scen_game_views

Figure 51: Displaying both scene and game views simultaneously

Adding a third-person controller

As we have seen in the previous section, Unity offers several types of character controllers to navigate the scene. While we have experimented with the First-Person Controller, it would be great to see how the second type (Third-Person Controller) works.

To add a Third-Person Controller, the process will be similar as the one we have followed so far, as we will add a corresponding prefab (e.g., a standard asset) to the scene.

If you are using Unity 5: locate the folder Assets > Standard Assets > Characters > ThirdPersonCharacter > Prefabs and drag and drop the icon labeled ThirdPersonController to the scene.

adding_3rdPCunity5

Figure 52: Adding the Third-Person Controller in Unity 5

If you are using a previous version of Unity (e.g., 4.5): locate the folder Assets > Standard Assets > Character Controllers and drag and drop the icon labeled 3rd Person Controller on to the scene.

adding_3rdPCunity4x

Figure 53: Adding a First person Controller in Unity 4.x

  • Once this is done we can move the FPS controller above the ground in the scene view and check how the game looks like in the game view.
  • Before doing so, we will deactivate the First Person Controller that we have included previously, to avoid any confusion between cameras. To do so we just need to select the First-Person Controller in the hierarchy, and, using the inspector, deselect the box to the left of its label.

If you are using Unity 4.x, you are pretty much ready to go and you can play the scene. However, if you are using Unity 5.x, there are a few more things we need to do in order to see our character.

As it is, our new character, while it will be able to move in all directions, will not be displayed onscreen, as it does not yet have a camera that will follow its movement. To do so, we need to import a package that includes cameras that have the ability to follow a specific target. In our case, we would like it to follow the character. Thankfully, Unity includes a set of assets included in the package called Camera that can do just that. Let’s import such a camera and use it to follow our character:

  • Select: Assets | import Package | Cameras.
  • A new window should appear that describes all the assets (scripts and object) included in this package. As we will be importing all of these, click Import.
  • After a few seconds, the package should be imported. Its content will be located in the folder Assets > Standard Assets > Cameras.
  • In the project window, locate the folder Assets > Standard Assets > Cameras > Prefabs as described on the next figure.

importing_camera

Figure 54: Importing camera assets

  • As you can see, this folder includes four types of camera. For now, we will be using the camera prefab called MultipurposeCameraRig. This type of camera can be used to follow a particular target.
  • Drag and drop the camera prefab labeled MultipurposeCameraRig to the Scene view.
  • Select this object (i.e., the camera) in the Hierarchy view as described in the next figure.

adding_camera

Figure 55: Adding the camera to the scene

As you have added the camera to the scene, you may notice, looking at the Inspector view, a component called Auto Cam (Script). Within this component, you may see an attribute called Target that is currently empty. This field refers to the object that we want to track with the camera. So to track the Third-Person Controller, we will need to drag and drop it from the hierarchy view to this field (i.e., Target) as illustrated on the next figure.

setting_camera_target

Figure 56: Setting the target for the camera

After making these modifications, it is time to test the scene by pressing the Play button or alternatively pressing the shortcut CTRL+P. before you pay the scene, just make sure that both the First-Person Controller and the Main Camera are deactivated.

As we play the scene, we can navigate using the arrow keys from the keyboard, we can jump (using the SPACE key), look around using the mouse, or run by pressing the arrow key while also pressing the SHIFT key.

playing_secne-with_3rdPC

Figure 57: Playing the scene with the 3rd Person Controller

>> ALMOST THERE… LET’S GO TO PART7!

Related Articles: