How To Make Your NPC Move

Step 1: Open your NPC’s Prefab by double-clicking it in the project window. If you don’t have a prefab of your NPC, follow this guide for importing your NPC from Blender into Unity.

You will know that you are in the prefab editor when the background in your scene view is blue.

Prefab Editor Mode

We are going to edit our NPC in prefab editor mode rather than directly in the scene because this allows us to make direct changes to the prefab. If we were to work in the scene instead, any changes we made to the prefab would later have to be saved to the prefab. Using prefab editor mode lets us skip that extra step and allows us to directly edit the prefab.


Step 2: Add the Nav Mesh Agent Component to your Prefab’s parent GameObject.

  1. Select the parent GameObject.
  2. In the Inspector window, click Add Component.
  3. Search for Nav Mesh Agent and select it.

Step 3: Enable Gizoms so you can see your Nav Mesh Agent‘s Obstacle Avoidance green cylinder.

  1. Ensure that Gizmos is on by clicking the Gizmos icon in the toolbar.
  2. Now when you have the parent GameObject selected, you should see a green cylindrical outline on your NPC.

Step 4: Ensure that the bottom of the green cylinder matches the bottom of your NPC. If this is not already the case, follow these steps:

Why do we need to do this?

If you skip this step, then you may notice your NPC will be halfway in the ground when it tries to move during play mode. We need to ensure that the Nav Mesh Agent knows where our model’s feet are.

  1. Select the GameObject holding your NPC‘s model.
  2. Click the Lock button in the inspector to lock the inspector to your model’s GameObject.
  3. Select the parent GameObject.
  4. Adjust your model’s Transform‘s Y position until the bottom of the NPC matches that of the green cylinder.
  5. Once you’ve finished, you can unlock the inspector by clicking the Lock button again.
About locking the Inspector window.

Usually, when we select an object in the Hierarchy window, it also selects that object in the Inspector window. However, there are certain times when we want to be able to select a GameObject in the Hierarchy, but edit a different GameObject in the Inspector at the same time.

In this case, in order to see the green cylinder, we need to select the parent GameObject. However, we want to edit the child GameObject at the same time. So, we can lock the child GameObject in the Inspector, then select the parent GameObject, and edit the child GameObject at the same time.


Step 5: Adjust the Radius and Height values of your Nav Mesh Agent‘s Obstacle Avoidance to match your NPC’s model.

  1. Select the parent GameObject.
  2. Adjust the Radius and Height values until the green cylinder covers your NPC
Tip: Don’t make your radius and height too big.

Making your radius and height too big can make it harder for your NPC to move around in tight spaces.


Step 6: Add the Patrol component to your NPC’s parent GameObject.

This will let us add points for the NPC to walk to later.


Step 7: Add and configure the Nav Mesh Surface component to your floor.

  1. Enter the scene you want to work in. If you don’t know which scene to work in, then enter the Barebones Scene located in Assets/Pre-College/Scenes.
  2. Select the floor your NPC will be traversing.
  3. In the Inspector Window, click Add Component, then search for and add a Nav Mesh Surface component.
  4. Change the Use Geometry variable in the Nav Mesh Surface component from Render Meshes to Physics Colliders.
  5. Click Bake (this process may take some time depending on how complex your floor is).
  6. Once the Bake process is finished, with Gizmos on, you should see blue spots on your floor. These areas are where your NPC is able to traverse.

Step 8: Add patrol points so your NPC can move around.

  1. In your --- NPC --- folder, create a new empty GameObject called Patrol Points.
  2. Create a new cube named Patrol Point. Disable its Box Collider component.
  3. Duplicate the Patrol Point object you made by selecting the Patrol Point object and pressing Control and D on your keyboard. Then move your new Patrol Point (1) object to a different position.
  4. Assign the Patrol Point objects you made to the Points variable in the Patrol component on your NPC’s parent GameObject by clicking the plus (+) icon and dragging the Patrol Point GameObjects from the Hierarchy Window into the Inspector Window.
  5. Enter Play Mode, and switch back to scene view and observe your NPC moving between the points you assigned.
  6. You can disable the Mesh Renderer component on your Patrol Points so your player does not see them. It’s just useful to have them be visible while you’re developing.

Step 9 (Optional): Make it so that your player “Loses” when they come into contact with your NPC.

  1. Go back into prefab editing mode by double-clicking your NPC’s Prefab in the Project Window.
  2. Create a new empty GameObject and name it Player Collider.
  3. Give it a Box Collider or other collider depending on the shape you’d like to use, and edit the collider until it covers your whole NPC.
  4. Enable the IsTrigger variable in the Collider component.
    • This disables normal physical collisions and has the collider act as just a sensor for coming into contact with other colliders.
  5. Add the Enemy NPC script to the Player Collider GameObject.
  6. When your player comes into contact with your NPC, the player should now “lose.” (Make sure that the Sandbox Mode variable in your Experience Manager is not enabled when testing this)

Next Steps:

  1. Create win objects
  2. Create a win container