Home and Learn: Games Programming Course
In this tutorial, you'll learn how to get game characters to move around all by themselves. This comes under the heading of AI NAVIGATION. You can set a character to follow and chase the player, for example, or just have some guards patrolling a particular area. Let's see how it's done. First, make sure to create a new Unity project for this.
The first thing to do is to load the package we need. Once your new project
has loaded, click the Window menu at the top of Unity. From the
Window menu, select Package Manager. From the Package Manager screen,
make sure to change the dropdown in the top left to Unity Registry.
In the search box on the right, type ai:
Now click the Install button top right. Once the AI NAVIGATION package has been installed, click the Samples folder and import those:
Once you have installed the AI NAVIGATION package and the samples, you can close down the package manager. If you look at the Project Area at the bottom of Unity, you should see these new folders:
If you want, expand the folder called Scenes from the Samples folder:
As you can see, there are lots of sample scenes you can double click to load. Play the samples to get a feel for what AI Navigation can do.
Let's build our own scene. We'll create a simple scene for this using a prefab from the Sample folder. So, locate the heightmesh prefab, which is in the following folder:
Assets/Samples/AI Navigation/1.1.5/Build And Connect NavMesh Surfaces/Models
It should look like this in the projects area of Unity:
Drag and drop the model into your scene.
The Samples folder you imported also contains some ready-made Materials. Drag and drop a Material onto your height mesh. We've gone for the bright green material in the image below:
With the scene set, we can now add our AI Navigation.
In order for your characters to move around by themselves, you need to add something called a NavMesh Surface (you can add more than one NavMeshSurface).
In the Hierarchy on the left, right-click. From the menu that appears, select AI > NavMesh Surface:
When you add a NavMesh Surface, notice the Inspector that appears on the right of Unity:
We'll get to what all these settings do later. Let's leave them on the defaults.
Now that the AI characters have a surface to walk on, we need to add a character that will do the walking.
We'll just use a simple capsule. So, select 3D Object > Capsule from the GameObject menu at the top of UNITY. (You can also right-click in the Hierarchy on the left and add a capsule from there.)
Position the capsule near the bottom of the stairs and slope.
We also need to add a destination for the character to move to. So add
another 3D Object. This time, add a Cube. Position the cube at top of
the stairs, somewhere near the arch. Resize by changing the Scale Y
value in the Inspector on the right to 0.1. You can add a material
to your cube, as well. Your scene should now look something like this:
AI Characters that you want to move around by themselves need a NavMeshAgent component added to them. So, select your Capsule. In the Inspector on the right, click the Add Component button. Type nav in the search box and select the Nav Mesh Agent item:
When you add the Nav Mesh Agent, it should look like this:
Again, there are lots of settings to explore for a NavMesh Agent. But leave them on the defaults. We'll come back to them.
Before Unity can understand which agents can walk where, you need to Bake the NavMeshSurface. To do that, click back on your NavMeshSurface in the Hierarchy on the left. In the Inspector on the right, click the Bake button:
Before Unity can understand which agents can walk where, you need to Bake the NavMeshSurface. To do that, click back on your NavMeshSurface in the Hierarchy on the left. In the Inspector on the right, click the Bake button:
(If you later add more objects to your scene, you can click the Clear button and then re-bake by clicking Bake button again.)
When you click Bake, you should see a blue outline of where your AI characters can walk:
We'll come back to this and change some settings. For now let's add a script to get the character to move to our destination.
Click on your Capsule to select it. In the Inspector on the right, click the Add Component button again. In the search box, type C#. Select the New Script item:
Type MoveToDestination as the name of the script. Then click Create and Add:
When the script is added, double click to open it up in the code editor:
Add the following few lines:
public GameObject target;
void Start()
{
var NavMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
NavMeshAgent.SetDestination(target.transform.position);
}
Your script should look like this:
Save the script and then go back to Unity. In the Inspector on the right, notice that there is now an empty slot below the script name:
It's called Target because that's the name we used in the script:
public GameObject target;
The other two lines in the code first get a reference to the NavMesgAgent:
var NavMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
Then we use SetDestination to move the agent:
NavMeshAgent.SetDestination(target.transform.position);
In other words, get the NavMeshAgent component attached to our Capsule. Then set the destination as that cube we added.
But click the circle to the right of the Target item. From the
box that pops up, select your Cube (we left all our game objects
on their default names):
(You can also drag and drop your Cube from the Hierarchy to the empty Target slot, instead of selecting from the popup box.)
You can now play your game. Click the Play button and watch the Capsule walk up the stairs and stop at your cube.
If your camera is positioned badly, you'll have to select Main Camera in the Hierarchy. In the Game window rather than the Scene window, change the following values in the Inspector:
Position X: Move the camera left
to right
Position Y: Move the camera up and down
Position Z: Move the camera backwards and forwards
Rotation Y: Rotate the camera on the Y axis (up and down)
Now move your destination cube to top of the slope. Click Play again and your Capsule character will head on up the slope rather than the stairs.
In the next lesson below, we'll take a look at some of the settings for the Nav Mesh Agent and the Nav Mesh Surface.
<--Back to the Unity 3D Course Contents Page
Email us: enquiry at homeandlearn.co.uk