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

Artificially intelligent Ethan

To start, we want to replace the ThirdPersonController prefab that we used initially with Unity's AI character, AIThirdPersonController, using the following steps. Unity uses the word artificial intelligence loosely to mean script-driven. Perform the following steps:

  1. Open the Unity project from the previous chapters with the Diorama scene, and have the Characters package imported from Standard Assets.
  2. In the Project panel, open the Standard Assets/Characters/ThirdPersonCharacter/Prefabs folder and drag AIThirdPersonController into the scene. Name it Ethan.
  3. In the Hierarchy panel (or in Scene), select the previous ThirdPersonController, (the old Ethan). Then, in the Inspector panel's Transform pane, choose the gear icon on the upper right of the Transform pane and select Copy Component.
  4. Select the new Ethan object (from the Hierarchy panel or Scene). Then, in the Inspector panel's Transform pane, choose the gear icon and select Paste Component Values.
  5. Now, you can delete the old Ethan object by selecting it from the Hierarchy panel, right-clicking to open options, and clicking on Delete.

If you cannot find the Characters package to import, you may not have installed Standard Assets when you installed Unity. To get them now, you will need to run the UnityDownloadAssistant again as described at the beginning of Chapter 2Content, Objects, and Scale (and it may already be in your Downloads folder).

Note that this controller has a NavMesh Agent component and an AICharacterControl script. The NavMesh Agent has parameters for how Ethan will move around the scene. The AICharacterControl script takes a target object where Ethan will walk to. Let's populate that, as follows:

  1. Add an empty game object to the Hierarchy panel, and rename it WalkTarget.
  2. Reset its Transform values to position (0,0,0) (using the gear icon in upper-right of the Transform pane).
  3. Select Ethan and drag WalkTarget into the Target property in the Inspector panel's AI Character Control pane, as shown here:

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