Nerd For Tech
Published in

Nerd For Tech

Creating Modular Waypoint System in Unity

Stealth Game & Cinematography

Objective: Create a modular way point

To create a modular way point, lets first make waypoints in the scene view. Create a gameobject and place it in the location your player is going to go. Also take note that the player is going to take the shortest past so make sure to add points in between if you want your player to moves through specific direction. Also add a nav mesh agent to the guard to navigate in the nav mesh.

Now create a Guard AI script since we are creating a waypoints for guards to move at.

Inside the script, create a reference variable to a nav mesh agent added to the guard. Also, create a List of Transform variable that is going to store transform value of each way points.

Then create a int variable which will define which waypoint to access in the list. Then create two bool variables, one to define if the guard is moving to the point where is tarted or is it going to the defined location and other to check if the target has been reached or not.

Now create a reference to the nav mesh agent on the start.

Inside Update, we are going to check if the guard has way points or not as well as if there is a waypoint in list at current target index.

If both returns true then set the destination of guard to the waypoint. We have a animation where guard walks and idles so if the guard reached target then set animation to idle, if not set animation to walking.

Now check the distance between target position and current position and if the distance is less that 1 the guard is going to call a coroutine.

Now inside the coroutine, set the time to make the guard to idle at start and finish for random number between 2 and 5 sec. Then check the waypoint position in the list. If index is last then set reverse bool to true else false. Also set the Auto braking to true so that guards do not move around.

Then check if the guard is moving to finish or start and depending on it increase or decrease the index value in the form of current target.

Now set the target reached to false as the guard starts moving again.

Drag all the waypoints in the list for guard to move towards. Also note that each guard will have the same script but they function as their own so they store different waypoints so their destination is also going to be different.

And this is the final look.

--

--

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