Enemy AI in Unity

Unity has an AI system built in. So you can have game objects like enemies wander around all over the place without too much effort on your part. What we'll do is to have a soldier run to a particular spot and then start shooting at us. We'll even get him to run around obstacles.

If you're coming from the first person shooter lessons, then simply create a new scene in your Scenes folder (right click, Create > Scene). If you haven't done the FPS lessons, then see here on how to download and import the soldier model and the first person controller:

Import Assets from Unity Store

OK, let's get started.

We'll create a simple scene with Planes. So, with a fresh Scene, click Game Object from the menus at the top of Unity. From the Game Object menu, select 3D object > Plane. You can make it a little bit bigger by changing the Scale values to 2 for the X, Y, and Z. Make sure the Position and Rotation values are all on zero. Your Inspector for the Plane should look like this:

Inspector showing transform values

Add a material to your plane, just to give it a bit of color. Now we'll add another two Planes, to create a kind of crossroads. So, add another Plane. (Game Object > 3D Object > Plane.) For this Plane, change the Position and Scale to these values in the Inspector:

Position

X: 0
Y: 0.01
Z: 0

Scale

X: 2
Y: 1
Z: 0.5

Again, add a material for a bit of color.

Now duplicate this second plane. Change the Y Rotation to 90. You should then have something like this, in Scene view:

A Unity scene showing a plane

Now drag and drop your first person controller into the Scene. Change the Position and Scale to these values in the Inspector:

Position

X: 0
Y: 0
Z: -9

Scale

X: 1
Y: 1
Z: 1

Drag and drop a soldier onto your Scene. Change the Position and Rotation values in the Inspector to these:

Position

X: 0
Y: 0
Z: 8

Rotation

X: 0
Y: 180
Z: 0

It should look like this in Scene view:

A Unity scene showing a first person controller and a soldier model

What we want to do now is get the soldier to move by himself to a certain destination. For that, we can use AI. (We'll add the destinations shortly.)

 

Unity AI

The Unity AI is called NavMesh, short for Navigation Mesh. The mesh is an area you select that you want your game objects to move around on. You then apply a component called a NavMesh Agent and use code to specify where you want your game object to move to. It's a lot easier than it sounds. Let's see how it works.

To activate the AI system, click the Window menu at the top of Unity. From the Window menu, select AI > Navigation:

The Unity AI menu

When you click on Navigation, you should see a new tab appear on the right, next to the Inspector. This tab

The Navigation tab for Unity AI

Notice the four buttons at the top: Agents, Areas, Bake, Object. The Agents tab is showing, in the image above. An agent is whatever character or game object you want to make artificially intelligent. We don't need to worry about the settings here. But they allow you to specify a height and radius for your character, as well as the height of any steps they are expected to climb. If you have a slope in your scene, you can set the angle of the slope that the character is expected to negotiate.

The one we want is the Object tab. So click the Object button. The Navigation tab will change to this:

The Object page of the Navagation area

The idea here is to select any surfaces that you want your AI characters to walk around on. In the Hierarchy on the left, then, select the second and third planes you added, not the first Plane. For example, here's our Hierarchy and Scene with our crossroads selected. (Planes 1 and 2 in the Hierarchy):

A scene in Unity selecting areas for the AI

The grey areas in the image above are the only areas we want our soldier to walk on. We don't want him to walk on the grass.

With the two Planes selected, your Navigation tab on the right of the Inspector should then change to this:

The Object page showing planes selected

All areas you want your AI characters to navigate have to be made static. You can do that either by checking the Navigation Static option in the Navigation tab (check the Generate Off Mesh Links option as well):

Marking areas of the NavMesh as static

Or you can switch to the Inspector and check the Static option top right:

The Static box being indicated in the Inspector

With your crossroads marked as static, click the Bake button in the Navigation tab. Don't worry about the other options here. Just click the Bake button at the bottom:

The Bake button being indicated

What you are doing here is creating a baked area that Unity can load when your game starts. If your AI areas weren't baked in, Unity would have to calculate the areas constantly at run time, which would be very expensive in terms of processing power. But, anyway, note that the crossroads, your baked area, turns blue in Scene view:

Unity Scene view showing baked areas of a NavMesh

The blue area is your NavMesh. Notice, however, that the blue areas are too far from the edges, in our Scene. You can make them bigger by changing the Agent Radius value. The default is 0.5. Change this to 0.1. Now click the Bake button again in the bottom right:

Changing the Agent Radius for a NavMesh

The NavMesh should be a lot tighter to the edges:

The NavMesh area enlarged and baked

NavMesh Agent

OK, we have a crossroads we've designated as an area that an AI character can walk on. But we need a way to get the character to respond to the NavMesh. That's where NavMesh Agents come in. A NavMesh Agent is a component you can add to game objects. You then get that component with a bit of code. Once you have the component, you can set a destination for it. Your character will then move to that destination.

So, click on the Inspector tab. Select your Soldier in the Hierarchy. Now, in the Inspector, click the Add Component button at the bottom. In the search box, type Nav:

The Nav Mesh Agent menu in Unity

Select the NavMesh Agent item from the list to add it to your soldier character:

A Nav Mesh Agent in the Inspector

As you can see, there's a lot of settings you can tweak. We'll need to change a few of them soon. But now that we have an Agent that we can move around our NavMesh, we need somewhere to move him to.

You can use any game object you like as a destination for your AI character. It can be an object like a cube, an empty game object or even a player character. For example, you could make the player a destination for a zombie character and it would keep chasing you as you moved. However, what we want to do is to get out soldier to run to a particular spot, stop, and then start firing at us. We'll use a cube as our destination, just so that we can see where he's moving to. But in the real world, it's better to have an empty game object.

Right-click a blank area of your Hierarchy then. From the menu, select 3D Object > Cube. Rename it to Dest-1. Change the Position and Scale values in the Inspector to these:

Position

X: 0
Y: 0.01
Z: -2

Scale

X: 1
Y: 0.01
Z: 1

Add a material to give your cube a bit of color and it will look something like this in Game view:

A soldier model and a destination for the AI

In the next lesson below, we'll add code so that the soldier moves to that blue square. (We'll add another other soldier, as well, soon, and he'll come from the right.)

<--Back to the Unity 3D Course Contents Page