Nerd For Tech
Published in

Nerd For Tech

Waypoint-Patrolling AI With Branching Paths In Unity, Part 1

For my stealth project, I had to make patrolling guards. However, I decided to mix it up with some randomly-selected alternate paths, and a “look direction”.

Today’s Objective: Create a simple modular AI guard that will patrol along some waypoints, and sometimes choose 1 of 2 waypoints to go to.

Creating the system:

The Variables:

I create a new class called “AIWaypointPatroller” which will hold all the AI code for patrolling along a set of waypoints.

I want to allow for facing a specified direction while at each Waypoint, plus the option for alternate Waypoint positions (which I’ll add in Part 2), so I create a custom struct for the List<> instead of just a Vector3.

I also want to show the Waypoints in the Scene view for easy debugging.

Finally, the private variables:

The Functions:

Awake:

Update:

Functions called during Update:

Finally, to draw the nice debug lines in the Scene View, make an OnDrawGizmos() method:

There are 3 parts I want to draw:

  • The current position,
  • The Waypoints and the lines between them,
  • And the Look Direction of each Waypoint.

The Result:

Finally, all that work pays off with a very nice looking waypoint system!

A waypoint path

In Part 2, my next post, I will add the possibility for alternate paths, and add functionality for turning slowly to face the LookDirection.

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Let’s Write an IPython Extension the Hard Way

Why learning to code makes you feel incompetent — and ways to combat it

Introduction to git.

Databases from the ground up Part II

Writing Smart Contracts…

Walter White in the lab

C# LINQ — “Distinct”

IoT: Particle with MQTT

AI in Product Engineering

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
Vincent Taylor

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.

More from Medium

All about Sky in Unity’s HDRP

Indy Pro ’22: Distance System

Cookies and Lights in Unity 2020

Using Unity’s ProBuilder To Model A Sci-Fi Container