Adding and Managing Simple Artificial Intelligence with Unity

In this section we will discover how to add NPCs and give them basic intelligence so that they can follow the player .

 

Adding and setting-up our first NPC

First, let’s add an NPC to the scene.

  • Open the indoor scene from your project (i.e., maze).
  • You may deactivate the ceiling object so that it is easier to add objects to the scene.
  • You can set the Ambient Light Intensity to 1, so that it is easier to see the scene (Window | Lighting | Settings).
  • You can also display the view from above by clicking on the green arm (y-axis) of the gizmo.
  • Open the Project window, and navigate to the folder: Standard Assets | Characters | ThirdPersonCharacter| Prefabs.
  • In this folder, you should see two objects; one of them should be labeled AIThirdPersonController. It corresponds to a character that you can use as an NPC (after some light configuration).
  • Drag and drop this prefab (i.e., AIThirdPersonController) to the scene, and place it just above the ground, relatively away from the player, so that it needs to cover some distance to get near the player.
  • This should create a new object in the Hierarchy window called AIThirdPersonController.
  • Once this is done, click once on this object in the Hierarchy and then look at the Inspector
  • This should display all components for this object, and you should see, amongst other things, that it includes a component called AICharacterControl, as illustrated on the next figure.

Figure 6‑1: Controlling the AI with a script

  • As you can see, this component is a script with a public variable called Target. This variable Target is empty for now, and its type is Transform. The Target variable is linked to the target for the NPC. So, by specifying the target we can effectively tell Unity what object the NPC will be following (or targeting). The only thing we need to do is to drag and drop the object that we want the NPC to follow on top of this field (i.e., Target). In our case, the target is the player. So we need to drag and drop the object for the player (the object labeled FPSController; this object is located in the maze folder within the Hierarchy window), to this empty field.
  • So, let’s proceed: Select the object
  • Drag and drop the object FPSController from the Hierarchy window to the empty field to the right of the variable Target, located in the AICharacterControl component of the object AIThirdPersonController.

Once this is done, the Target field for the object AICharacterControl should look as follows:

Figure 6‑2: Specifying a target for the NPC

Now, if we play the scene as it is, you should see that the NPC still does not move, although we have set a target. The reason for this is that, at this stage, the NPC does not “know” how to get to the player. So, some computations need to be performed so that Unity determines possible routes and nodes that the NPC can use to get (i.e., navigate) to its target. This is performed using the Navigation window. So let’s have a look at this window and finalize the navigation for the NPC.

  • Open the Navigation window (Window | Navigation).
  • This should open the window Navigation next to the Inspector

Figure 6‑3: The Navigation window

The idea now is to specify all objects that the NPC will be able to walk on or avoid, in order to get to the player. To do so, we will need to select these (e.g., in the Hierarchy window), specify that the NPC can either walk on or around these objects, and then “Bake” the scene. The “Baking” process will calculate possible “routes” that the NPC can take in order to reach its target. So, let’s try this:

  • Select the ground object in the Hierarchy
  • In the Navigation window, select the option Static Mesh, and leave the option Navigation Area to Walkable (i.e., default option).

Figure 6‑4: “Baking” the scene – part1

  • Once this is done, click on the tab called “Bake”, then click on the button labeled Bake located at the bottom of the Navigation window.

Figure 6‑5: Baking the scene – part2

  • This should display a progress bar, at the bottom of the Navigation window, that will disappear once the baking process is complete. At this stage, if you look at the ground, you should see that it looks blue, and that Unity has generated a few nodes (i.e., black dots).

We are almost there, as we just need to repeat the last steps with the other objects present in the scene, including the walls(but not the player). Please do the following:

  • Select each wall in the scene. To speed-up the process, you can also, in the Hierarchy window, search for the term cube, and this will return all cubes included in the scene (i.e., including walls). You can then select all these objects (e.g., using CTRL + Click).
  • Then select the option Navigation Static in the Navigation
  • Leave the other options as default and click on the button labeled Bake.

The Scene view may now look as follows:

Figure 6‑6: Displaying meshes and nodes after “Baking” the scene

At this stage we are ready to test our game:

  • Please play the scene.
  • Look at the Scene
  • See how the NPC is finding its way to the player.

Figure 6‑7: The NPC navigating towards the player

If you’d like to know more about Artificial Intelligence in Unity, you can download this FREE eBook Today!

Related Articles: