AI Pathing and NavMeshes with VR

Rushil Reddy

In this tutorial we are going to be exploring NavMeshes inside of Unity to take care of our zombies pathing.

First select the town object in the Hierarchy tab and go to the Inspector panel. Check the box named static, choose Yes, and choose change children. Now go to Window>Navigation and on the panel that opens choose bake. Change your Agent Height to 2 and click Bake.

We need to make sure our zombies use this map so they don’t run into/through any objects in our scene. On our zombie prefab click Add Component->Nav Mesh Agent. Change the speed to 2 and the acceleration to 5.

Now on the Zombie prefab, double click on the zombieController script to open it up for editing.

Add these lines at the top

private NavMeshAgent nav;

public Transform barrel;

//This should be inside the void Start() function

nav = GetComponent();

nav.destination = barrel.transform.position;

int count = 0;

In our update function were going to the following code to make sure the zombies update their positional tracking with our movements.

void Update() {

     if(count %10) {

           nav.destination = barrel.transform.position;

     }

     ++count;

}

 

Save the script and go back to Unity. Open up the camera rig> controller left> M4A1 Sopmod object. Now drag the barrel game object onto the appropriate space.

Hit Play and the zombie should now be following you wherever you go.