Unity 2020 Virtual Reality Projects
上QQ阅读APP看书,第一时间看更新

Adding Ethan, the walker

Gaming is a common application of virtual reality. So, we might as well start out from there, too! We are going to give our character, Ethan, a life of his own. Well, sort of (or not), because he's going to become a zombie!

We left off the Diorama scene at the end of Chapter 2, Understanding Unity, Content, and Scale, with Ethan hanging out. If, by chance, you skipped that part and don't have Ethan in your Project assets, import the UnityStandard Assetspackage (https://assetstore.unity. com/packages/essentials/asset-packs/standard-assets-for-unity-2017-3-32351 ) by following the instructions in theUsing Unity Legacy Standard Assets section in Chapter 2, Understanding Unity, Content, and Scale, in this book.You may need to delete any error scripts (that we won't be using) and convert imported materials into the current render pipeline. We then need to drag a copy of the ThirdPersonController version of Ethan from theProjectAssets/Standard Assets/Characters/ThirdPerson
Character/Prefabs/
folder. When you pressPlay, the Ethan character animates idly; press theW, A, S, and Dkeys on the keyboard to move him around.

Naturally, keyboard input is not appropriate for VR. In the next chapter, Chapter 5, Interacting with Your Hands, we will go into handheld input controllers. For now, we will consider another way to make him move around; that is, using the direction of your gaze while wearing your VR headset. Before we attempt this, we'll first transform Ethan into a zombie and have him walk around aimlessly without any user control. We'll do this by giving him some AI and writing a script that sends him to random target locations.

AI controllers and navmesh are somewhat advanced topics in Unity, but we're going to throw you into it just for fun. Besides, it's not as scary as zombies! Unity uses the term artificial intelligence in reference to intelligent pathfinding algorithms. A navmesh defines the area that an AI agent is allowed to walk in.

Before we begin, let's make sure we have a scene with an XR camera rig by following these steps:

  1. Open the Diorama screen we created earlier using File | Open Scene (or double-click the scene file in the Project window's Scenes/ folder).
  2. Add an XR Rig using GameObject | XR | Stationary XR Rig.
  3. Save the scene with a new name using File | Save As.

In the following sections, we are going to replace the Ethan third-person controller with an AI version of the controller, then define a position target where he should move to. Then, we'll generate a navigation mesh (navmesh) that defines the allowable walking area.

Artificially intelligent Ethan

To start, we want to replace the ThirdPersonController prefab that we used initially with Unity's AI character, AIThirdPersonController. We'll do this by inserting the AI version into the scene, copying the transform from the old character, and pasting those values into the new one. Follow these steps to do so:

  1. Start by opening the Unity project and Diorama scene we created in Chapter 2, Understanding Unity, Content, and Scale.
  2. In the Project window, open the Standard Assets/Characters/
    ThirdPersonCharacter/Prefabs
    folder and drag AIThirdPersonController into the scene. Name it Ethan.
  3. In the Hierarchy window, select the previous ThirdPersonController (the old Ethan), if present. Then, in Inspector, choose the three-dot icon to the upper right of the Transform panel and select Copy Component.
  1. Select the new Ethan object from the Hierarchy and on its InspectorTransform, choose the three-dot icon and select Paste Component Values.
  2. Now, you can delete the old ThirdPersonControllerobject from the scene (in Hierarchy, right-click | Delete).

The Transform context menu where you can copy/paste the values that were used in these steps can be seen in the following screenshot:

The Ethan object has Nav Mesh Agent and AI Character Control components. Nav Mesh Agent has parameters for how Ethan will move around the scene. On the other hand, AI Character Control takes a target object where Ethan will walk to. Let's populate this, as follows:

  1. Add an empty game object to the Hierarchy (GameObject | Create Empty) and rename itWalkTarget.
  2. Reset its Transform values to position (0,0,0) (Transform | right-click | Reset).
  1. Select Ethan and drag WalkTarget into the Target property in the Inspector panel's AI Character Control component, as shown here:

We now have an AI character in the scene (Ethan), an empty game object that will be initially used as a navigation target (WalkTarget) in the center of our scene, and we told AI Character Controller to use this target object. When we run the game, wherever WalkTarget is, Ethan will go there. But not yet.

It is common in Unity to use an empty game object just for its Transform component in order to specify a location in your scene. In this case, the WalkTarget's position will be used by the AI Character Control component.

Next, we'll add a NavMesh that defines the walkable areas in the scene.

The NavMesh bakery

Ethan cannot just go walking around without being told where he's allowed to roam! We need to define a navmesh, a simplified geometrical plane that enables a character to plot its path around obstacles.

In our scene, Ethan is a navmesh agent (he has a Nav Mesh Agent component attached). The navmesh says where he's allowed to walk. We'll create a navmesh by marking the static objects in the scene that should block navigation and then baking the navmesh. To do this, follow these steps:

  1. Open the Navigation window (Window | AI | Navigation). The Navigation window is shown in the following screenshot.
  2. Select its Object tab.
  3. Select Ground Plane in Hierarchy. Then, in the Navigation window, check the Navigation Static checkbox, as shown in the following screenshot. (Alternatively, you can use the object's Inspector window Static dropdown list.)
  4. Repeat step 3 for each of the objects that should get in his way; for example, in my scene, these are Crate, Ball, and UnitCube.
  5. In the Navigation window, select the Baketab.
  6. Unfold the Advanced options and check the Height Mesh checkbox (this will prevent Ethan from hovering above the ground).
  7. Then, click on theBakebutton at the bottom of the panel:

The Scene view should now show a blue overlay where the navmesh is defined, as shown in the following screenshot (if you don't see the blue navmesh areas, ensure the Navigation window is active, the Gizmos button is enabled at the top of your Scene window, and the Show NavMesh checkbox is checked in the lower-right of the Scene window):

To test this out, follow these steps:

  1. Ensure that the Game window's Maximize on Play setting is not selected.
  2. Click on the Play button (the triangle at the top of the editor).
  3. In the Hierarchy window, select the WalkTarget object and ensure that the Translate gizmo is active in the Scene panel (you can use the W key shortcut on the keyboard).
  4. Now, drag the red (X-axis) and/or the blue (Z-axis) arrow handles onto the WalkTarget object to move it around the floor plane. As you do, Ethan should follow!
  5. Click on Play again to stop Play mode.

Now, we have Ethan, the nav agent, a navmesh defined in the scene, and a target that Ethan automatically walks toward. Next, we'll write a script that moves the WalkTarget object to random places.