Day 76: Creating Modular Waypoint System in Unity

Objective: Create one script that any guard can use to move around.

Since we don’t want to have script for each and every guard that all have the same function but move different, we want to make this script modular in a way they all can use the same one.

To start off, we will need a list to hold all of the waypoints and their transform. We will declare a List of type Transform named _waypoints.

We will also need to keep track of which waypoint we are at, so therefore we need a variable of type int.

We need to bools as well. One bool is to know when the guard is at the next waypoint, this is named _targetReached. The other bool is to know when the guard has reached the last waypoint in the list. Once it hits the end it’ll become true to have the guard turn around, this is called _reverseWaypoints.

For our guards, we are moving them using the NavMeshAgent and Animator, so we have a reference to that as well and we use GetComponent, as seen before.

In our update, we need to check to see if our List of waypoints is bigger than zero and that the current target, waypoint, is not null. Doing this allows for any guard with this script to have no waypoints and not give out an error.

Inside the above If statement, we need to check the distance from the guard to the next waypoint. This will help the AI system know when to increase the _currentTargetIndex. We use the distance to handle the animations bool parameters, seen in yesterdays article.

If the distance is less than 1 unit, and check to see if the target reached is not equal to true, we will set the targetReached bool to true and start our coroutine.

Under the Update function we have an IEnumerator. This IEnumerator is to have the guards pause at the beginning and end of the waypoints before turning around. To start the Coroutine, we create a local float variable to get a random number between 3 and 5. This will give the pause before turning around.

Next, we will need to check and see if the guard is at the end or beginning of the waypoints. To check if we are at the beginning, we compare our _currentTargetIndex to 0. To see if we are at the end we compare our _currentTargetIndex to _waypoints.Count-1. We need to subtract 1 from the waypoints because a list will see that it has 4 items in it but to call an item it starts at zero and not 1. If you want the 2nd item in the list you will need to call 1 to get it.

We do these checks to start the yeild return new WaitForSeconds(randomYield), this is what cause the Guards to pause.

Next we need to check if we are going reverse or not. If we are not we than check to see if we are at the end of the waypoints, if we are go reverse and if not go to next waypoint. This is done by increase the _currentTargetIndex by one. Same steps if we are going reverse, check to see if we are at the beginning and if we are reverse back to normal and if not subtract one from the _currentTargetIndex.

Once this is ran, we turn _targetReached back to false to start the whole process over again.

This is how you can create a modular waypoint system, one script to control many different AI’s.

Thank you for reading and have a wonderful day!




I am passonate on learning to program and use Unity to become a skillful Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

MQTT over Mesh

How to set up your headless Raspberry pi

5 steps to escape Tutorial Hell to Programming Heaven

An edited image of elmo from children’s show Seasame street raising his arms in from of a fire with the text ‘I am in hell’

How To VKontakte Feed On Website — Try Now

How to call a Lambda Function via API Gateway on Aws Services from the mobile client by using…

Mad Libs Generator | Rookie Week of Python Day 03

Journey to the Cypress World

R-Planet guide to Uplift your land!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tyler Smallwood

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer

More from Medium

Creating A Modular Powerup System in Unity

Platformer: Player Animation Part 3 (continued)

Adding a Main Menu to our UI

Stamina System in Unity