Home and Learn: Games Programming Course
At the moment, our zombie from previous lessons just wanders off and does his own thing. What we'd like to do is to get the zombie to chase after the player. There are only a few lines of code to get this working. But we'll need to add a Nav Mesh Surface, and a Nav Mesh Agent. Easy enough to do.
We'll cover the Unity Nav Mesh in more detail in a later tutorial. For now, locate the Graveyard_Ground object in the Hierarchy on the left. This is the surface enemies and players walk around on.
With your Graveyard_Ground surface selected in the Hierarchy, click the Add Component button in the Inspector on the right. Type Nav into the search box. From the list, select NavMesh Surface.
If you don't have any Nav Mesh items, then you'll need to import the AI Navigation using the Unity Package Manager. So, from the menus at the top of Unity, go to Window > Package Manager. Select Unity Registry on the left. Type Nav into the search box. Install the item called AI Navigation (ours says Remove because we have already installed it):
Now try adding the Nav Mesh Surface to the Graveyard Ground again. You should see this:
We'll come back to this when we're ready to bake (note the Bake button bottom right). But you can leave everything on the default setting for the Nav Mesh Surface.
Once you have a Nav Mesh Surface, you can add Nav Mesh Agent components to all game characters and objects that you want to move around on your surface. It makes scripting easier, as well.
So, click on your zombie enemy in the Hierarchy on the left of Unity. In the Inspector on the right, click the Add Component button and add a Nav Mesh Agent:
It should look like this:
There are lots of settings you can tweak here, but the one to concentrate on is the Speed. It defaults to 3.5. Change this to a lower value to slow your zombie down. Try 1.5.
Don't play your game yet. Go back to your Graveyard_Ground object in the Hierarchy. In the Inspector on the right, click the Bake button. The whole of the Nav Mesh Surface may turn blue:
You can toggle the blue on and off by clicking the icon indicated by the red arrow in the image above. You'll then see this dialog box:
Uncheck Show NavMesh if you don't want to see any blue areas.
The blue areas, by the way, are locations where your characters with a Nav Mesh Agent attached can currently walk on. (Yes, this includes the church roof! But as this is not a Nav Mesh tutorial, we'll leave the inner workings of Unity's AI Navigation for a later tutorial.)
Now let's add a script.
Click back on your zombie enemy. Click the Add Component button again. This time add a New Script. Call it ZombieFollowPlayer:
Double click the Script name to open it up in your coding editor.
When your script opens up, replace the whole of the code with the following (you can just copy and paste this script):
using UnityEngine;
using UnityEngine.AI;
public class ZombieFollowPlayer : MonoBehaviour
{
private NavMeshAgent enemy;
public GameObject player;
void Start()
{
enemy = GetComponent<NavMeshAgent>();
}
void Update()
{
enemy.SetDestination(player.transform.position);
}
}
It should look like this in your editor:
Not much code. We have NavMeshAgent variable called enemy, and a GameObject variable called player. In the Start method, we get the NavMesh Agent Component we attached to the zombie. We store this in the enemy variable. In the Update method, we set the destination of the enemy. In between the round brackets of SetDestination, we pass in the position of the player by using its transform.
Save you code in the editor and go back to Unity. You should see an empty slot for the player:
The Game Object you need here is the Player Camera Root, which is under the Player object in the Hierarchy:
You can either drag and drop Player Camera Root from the Hierarchy onto the Player slot in the Inspector on the right. Or you can click the small circle and select from the box:
The zombie script component should look like this:
Play your game and test it out. You should find that your zombie follows you wherever you go.
However, kill the zombie and you'll notice a problem - although he is dead on the ground, he still seems to making a sound before he vanishes. (He'll also try to follow you as he's dead on the ground. But we're going to kill him off a lot faster in a later lesson, so don't worry about this.)
To cure this, go back to your KillZombie script. We need to get the Audio Source component we attached to the zombie. Then stop the audio from playing.
Add the 3 lines highlighted below:
Or you can copy and paste the whole of the following script in place of the one you have (if your script is not called KillZombie, change the name after public class):
using UnityEngine;
public class KillZombie : MonoBehaviour
{
private Animator zombieAnimator;
private AudioSource zombie_sfx;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Bullet")) {
zombieAnimator = GetComponent<Animator>();
zombieAnimator.Play("Die");
zombie_sfx = GetComponent<AudioSource>();
zombie_sfx.Stop();
Destroy(gameObject, 2.5f);
}
}
}
Play your game again. You should be able to kill the zombie and not have him groaning after he is dead.
However, there is another problem you may have noticed - the enemy can walk right through you! We'll fix that in a short lesson below.
Fix Enemy Walking Through Player -->
<--Back to the Unity 3D Course Contents Page
Email us: enquiry at homeandlearn.co.uk