Home and Learn: Games Programming Course
In this lesson, you'll learn how to get an AI character to jump up onto areas all by itself. If that's not clear, take a look at the short video below. The thing to notice is just where the grey enemy can jump in persuit of the player. It can jump up onto the green block, but not the yellow and red blocks. These are safe havens for the player. Let's see how it's done.
Create a simple scene for yourself. Ours has a Plane and three cubes in different sizes, each with a material. In the Hierarchy, we created an empty game object called PLATFORMS and placed all our cubes and the plane under this:
Now, from the package manager (Window > Package Manager), import the following:
-- AI Navigation (see previous
lesson for how to do this)
-- Mini First Person Conroller
If you haven't yet downloaded the Mini FPC, click Window > Asset Store and search for it there.
Drag and drop a Mini FPC into your scene. (Make sure you don't use the Minimal version.)
The FPC has its own camera, so you can delete Main Camera from the Hierarchy.
Now right-click in the Hierarchy. From the menu, select 3D Object > Capsule. This will be the enemy, so rename the capsule to Enemy. You can add a material to your capsule. We went with grey. Position your Enemy in the scene. It should look something like this:
And here's the hierarchy:
Test it out and check that you can wander around your scene.
If your player can't jump onto the cubes, change the Jump Strength in the Inspector from the default 2:
Now we can add two Nav Mesh Surfaces, one for the player and a separate one for the Enemy.
In the Hierarchy, click on the PLATFORMS empty game object to select it. In the Inspector on the right, click the Add Component button. Add a NavMesh Surface. You can leave everything on the defaults:
We need to add a new agent type for the enemy. Click the dropdown for Agent Type, where it says Humanoid. Select Open Agent Settings:
Click the Plus button and add a new type. Rename it to EnemyAgent:
If you wanted to, you could change some values here. For example, If you had a tall, wide enemy, you could change the Height and the Radius. That way, a player could go under a bridge and the enemy couldn't.
But leave them on the defaults.
With the PLATFORMS object still selected in the Hierarchy, move to the Inspector on the right and add a second NavMesh Surface component. Change the Agent Type to EnemyAgent:
Click the Bake button on both NavMesh Surfaces. (The reason for having two nav mesh surafces is that each surface has its own settings. This keeps the player and enemy data separate.)
You should see a blue outline appear in your scene:
We can now add a simple script to the enemy so that he follows the player. Click Add Component and then type C# Script. Select New Script and call it MoveEnemyToDestination. In the editor, add the following:
There are only two line, one in the Start method:
enemy = GetComponent<UnityEngine.AI.NavMeshAgent>();
and one in Update:
enemy.SetDestination(target.transform.position);
We also have two variables set up at the top:
public GameObject target;
private NavMeshAgent enemy;
The line in Start gets the NavMesh agent from the enemy. The line in Update sets the destination for the enemy, which is just the target variable.
Save the code and go back to Unity. Select the first person controller as the Target variable:
Before you can play your game, you need to add a Nav Mesh Agent to the Enemy. So, select the Enemy in the Hierarchy on the left. In the Inspector on the right, click the Add Component button. Add a Nav Mesh Agent. Change the Agent Type from Humanoid to EnemyAgent. Leave all the other settings on their defaults:
If you play your game now, you'll see that enemy follows the player. But jump up onto one of your cubes. You'll see that the enemy tries to follow the player still. But it can't jump up onto the cubes. We'd like to change that.
The trick to getting an AI enemy to jump up onto things is by adding a game object called a Nav Mesh Link.
In the Hierarchy on the left, right click on a blank area. From the menu that appears, select AI > Nav Mesh Link:
Your Hierarchy shoul look like this:
If you zoom in on your scene, you should see the icon for a NavMesh Link:
Now have a look at the Inspector on the right:
Change the Agent Type from Humanoid to EnemyAgent. And notice that there are settings for Start Point and End Point. This is the starting point and ending for the enemy's jump. You usually want the start point on the floor and end point on a platform.
Play around with start and end points. When you change the values, especially the Y points, you'll see a black circles for the start and end points:
We got these points by changing our points to these (but yours will differ):
Play your game again. Jump up onto your green platform. You should see that the enemy can now follow you there. It will jump onto the platform all by itself. It does this because of a settings in the NavMeh Agent attached to the Enemy game object Auto Traverse OffMesh Link. Make sure yours is selected, if the enemy doesn't jump onto the platform:
You can add as many NavMesh Links as you like to your scenes, and position them whatever you want. So if you need an AI character to jump around, hopping up and down on platforms, it's NavMesh Links that you need.
<--Back to the Unity 3D Course Contents Page
Email us: enquiry at homeandlearn.co.uk