Creating an NPC

Creating a complex non-playing character can be a hard task. You can find the ready-made oldman NPC in the code book folder, but we will practice the Editor, by deconstructing the AI character prefab from Unity Standard Assets to build our own, and modify the structure and the code for our needs.

We are going to use a copy of our Chapter6-start.unity scene that we edited in the previous chapter, where the terrain is half-done, for simplicity, as this scene doesn't contain any additional buildings or other game elements. Open the scene and save it as Chapter8-NPC.unity. In this scene, you will find some of Unity's standard assets, such as the fire particle system that we will explore later, and terrain features such as trees and grass in a specific spot of the island in the scene. We will arrange our actors here, in this corner of the terrain, along with some trees and grass.

First of all, we want to make sure that the oldman rig is correctly imported as a Humanoid rig and that the animation clip included in the book's code is imported and set for the correct root motion animation, as we did in the final part of the previous chapter for the player hero.

We should also remember the avatar reference for all of the clips, which should be the avatars of the non-animated rig we did for our hero model back in Chapter 5, Character Animation with Unity:

All the animation clips that we need for the NPC will be set like the preceding screenshot, with the exclusion of the walk_in_cirle and the sitting clips, the former will have the Bake Into Pose box unchecked in the Root Transform Position (XZ) position panel, while the latter will have the Bake Into Pose option checked, will be based on the Center of Mass, and the animation will not be played in a loop. The same goes for the standup animation clip.

They will all need the Root Transform Position (Y) set on the feet in the original location and will need the option: Bake Into Pose disabled to avoid movement from the GameObject's original position:

You can check the ready-made settings on this animation clip in the assets folder, be careful when playing with this settings, as a wrong setting may result in unwanted unexpected results.

We will create the basic NPC, starting with the AIThirdPersonController prefab. You can do this in the Scene view, or you can switch to a front orthographic view to better check its body measures. Let's follow these steps:

  1. Drag in the scene the AIThirdPersonController prefab located in the Assets\Standard Assets\Characters\ThirdPersonCharacter\Prefabs folder.
  1. Drag our OldMan character model from the folder situated in Assets\Chapters7-9-10\Models\Characters and check its measures with the AIThirdPersonController prefab measure. Scale it accordingly to have the same proportion as the Ethan rig from the Standard Assets Characters package.
  2. Then we will drag our OldMan in the Hierarchy so that it becomes a child of the AIThirdPersonController GameObject:
  3. We will DELETE the EthanBody, EthanGlasses, and EthanSkeleton GameObjects from the Hierarchy.
  4. When prompted by Unity with the following popup, choose Continue:
The prompt asking if you are sure you want to lose connection with the stored prefab

The link for the old prefab will break, but this is okay, as we are building a brand new prefab for the old man NPC. The GameObject in the Hierarchy has now turned to black and is not linked to any saved prefab in the project anymore. So finally:

  1. Rename the GameObject from AIThirdPerson to OldMan (NPC).
  2. Check the size twice by looking at the characters from various angles in order to be sure that we have used the correct scale. Using orthographic front, top, or side views to check sizes can help, see the next screenshot.
  3. Remove the AICharacterControl component to leave some space for our own component.
  4. Create a new component and call it SimpleAI. This class will be the base to manage the NPC and the enemies AI.

Now we are ready to store the prefab we created. Drag the GameObject into a Prefab folder in your project, and you should see that the OldMan (NPC) GameObject in the hierarchy turns from black to blue.