The Great Fleece Development Recap (Part 2)
Recently I completed the 2nd project of my GamedevHQ course, The Great Fleece. I’d like to talk a bit more about its development.
The Guard AI Waypoint-Patrol System:
In this project, I needed a way for the guards to walk back and forth along a predefined route which included n-number of mid-points at which the guard would pause. A common solution for this kind of requirement is a waypoint system for the guards to path-find to, which is what I decided to create.
The system simply uses a list of world positions and a Unity Navigation NavMeshAgent component to allow the guard (or any entity) to path-find to each point. Using variable timers and options, I can choose how the patrol behaviour functions.
For some of the guards, I also needed the ability for them to walk to one-of-two waypoints randomly, to add some diversity to their patrols. For that, I gave each waypoint an optional “Alternative” point.
For easy development, I made the patrol component draw lines between each point. Green between no-alternative points, blue when one-or-more points has an active alternate. Finally, the red lines with the dot-end are the direction the guard will turn to at that point, and the red lines over the blue are the zone in which the guard will start turning prior to reaching the point (for smoothness).
Read more here.
The Player navigation/movement code is pretty simple. It just uses a raycast from the camera to the point where the mouse clicked (if a valid layer is there) to determine the destination. It then uses NavMesh.SamplePosition to find the closest valid NavMesh point near the mouse click (this ensures that the player avoids walking into walls or display cases. Then it uses the Unity Navigation system, like the guards, to path-find to that point.
Using the NavMeshAgent’s path while walking and a LineRenderer, I create an accurate line showing the path that the player will walk to the destination. I also enable a small 2D plane with a PNG marker and set its position to the destination.