Creating modular waypoint system in Unity

  • Create an empty object. Reset it. Rename it as Waypoints
  • Select a guard. Duplicate it.
  • Move the last one to the desired position. Copy component values
  • Create an empty gameobject in Waypoints. Paste component values
  • Repeat over and over as much as needed
orange: waypoints
  • Create a new script called GuardAI
  • Create a list of type Transform called waypoints
  • Declare a new variable currentTarget
  • In Start, check if null and set currentTarget as Waypoints[0]
  • Declare a NavMeshAgent. Set its destination
public List<Transform> waypoints = new List<Transform>();
public Transform currentTarget;
private NavMeshAgent guardAgent;

private void Start()
{
if (waypoints.Count > 0)
if (waypoints != null)
{
currentTarget = waypoints[0];
guardAgent.SetDestination(currentTarget.position);
}
}
  • Attach this script to all the AI guards
  • Place each waypoint in the guards’ lists
  • Set the waypoints index as int currentTarget
  • Increase and decrease its value with a boolean variable bool reverse
  • Set the guard to wait for some seconds after the waypoint has been reached using a bool targetReached and IEnumerator WaitBeforeMoving()

--

--

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