How do I get my patrolling enemies to go back to the patrol point they had not gotten to because they chased the player?

I know, it’s a very confusing question. Some of my code is from the Unity Manual, since I am not good with AI. I have been programming for about 2-3 years, it is a hobby; however, I am not very good in my own eyes. I know there will be areas that don’t make sense, or will make you think “Why is he calling that multiple times when it only needs to be called once?” or “His code is not neat.” Alright, sob story over.

Here’s my issue:

  1. My enemy AI patrol 5 points around a wall (Just to test the AI)
  2. If the player gets close enough, the AI chases the player-and goes to its last known location if the player is out of sight or is in the Shadow Realm. (The AI ignore the player if they are in the shadow realm)
  3. After they are done chasing the player, they go to the patrolPoint that is past the original patrolPoint they were going towards before chasing.

I want the AI to:

  • Patrol a set path
  • Store the patrol point they had not gotten to because of chasing the player
  • After losing the player, the agent.destination is the Patrol point they had not gotten to because of having to chase the player.

Here is my Code:

using System;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.AI;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Enemy : MonoBehaviour
 {
     // Public Variables (GameObjects, int, etc. Keep it clean...)
     public Transform[] patrolPoints;
     public ShadowRealm sr;
 
 
     // Patrol Variables
     NavMeshAgent agent;
     Transform target;
 
 
     // Private Variables (Same as Public but cannot be accessed outside of script)
     bool playerIsCloseEnough;
     int destPoint = 0;
     float distance;
 
 
     // Public Voids
     public void Start()
     {
         target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
         agent = GetComponent<NavMeshAgent>();
         agent.autoBraking = false;
         Patrol();
     }
 
     public void Update()
     {
         // Checks distance from player and this transform
         CheckDistance(); 
         
         distance = Vector3.Distance(this.transform.position, target.position);
         
         // if player is within the minimum distance
         if (distance <= 15) playerIsCloseEnough = true;
         if (distance >= 16) playerIsCloseEnough = false;
     }
     
     void CheckDistance()
     {   // If player is within minimum distance
         if (playerIsCloseEnough == true)
         {
             // if player is in Shadow Realm, player is not chased
             if (sr.isInShadowRealm == true) Patrol();
             if (sr.isInShadowRealm == false) Chase();
         }
         else Patrol();
     }
 
     void Chase()
     {
         agent.destination = target.transform.position;
         transform.LookAt(target.position);
         if (playerIsCloseEnough == false) Patrol();
     }
 
 
 
     void Patrol()
     {
         if (!agent.pathPending && playerIsCloseEnough && sr.isInShadowRealm == true && agent.remainingDistance < 0.5f) GoToNextPoint();
 
         // Choose the next destination point when the agent gets close to the current one.
         if (!agent.pathPending && playerIsCloseEnough == false && agent.remainingDistance < 0.5f) GoToNextPoint();
     }
     
     void GoToNextPoint()
     {
         // Returns if no points are set up
         if (patrolPoints.Length == 0) return;
 
         // Sets the agent to go to the current selected point
         agent.destination = patrolPoints[destPoint].position;
 
         // Choose the next point in the array as the destination, cycling to the start if necessary
         destPoint = (destPoint + 1) % patrolPoints.Length;
     }
 
     
 
 
     // Private Voids
 
 
 
 }

didnt read all that but if u divide ur path into sections, for example, the corners like u have in the illustration, when a guard reaches that checkpoint you can save taht as the most recent checkpoint. if guard chased the player then stopped, move to most recent checkpoint then continue on the path.