Method call is not working correctly in AI code

Hey, I’m brand new into the world of programming and I’ve been stuck on this problem for a while now.

I’m trying to make an AI system that would cycle through specified waypoint, but then initiate chasing down the player once the player enters the trigger and then resume cycling through the waypoints once the player leaves the trigger.

I’m essentially trying to make a chase script and patrol script work together and I have succeeded to some extent. The problem is, when the AI goes to the first waypoint, it gets stuck on it and doesn’t move on to the next unless the player walks in and out of the trigger.

I am using booleans to enable and disable in each function and the debug logs also do show them working as intended.

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;
using UnityEngineInternal;

public class AILocomotion : MonoBehaviour
{
    public Transform playerTransform;
    NavMeshAgent agent;
    Animator animator;
    public float maxTime = 1.0f;
    public float maxDistance = 1.0f;
    float timer = 0.0f;
    bool moveTowards = false;
    bool followWaypoint = false;
    float deaccel= 0.5f;
    float calVelocity = 0.0f;
    public Transform[] points;
    private int destPoint = 0;
    public string animState;

    void Start()
    {

        agent = GetComponent<NavMeshAgent>();
        animator = GetComponent<Animator>();
        moveTowards = false;
        followWaypoint = true;  
        
    }

    // Update is called once per frame
    void Update()
    {
        if (moveTowards == true)
        {
            timer -= Time.deltaTime;
            if (timer < 0.0f)
            {
                float sqDistance = (playerTransform.position - agent.destination).sqrMagnitude;
                if (sqDistance > maxDistance * maxDistance)
                {
                    agent.destination = playerTransform.position;
                }
            }
            animator.SetFloat("Speed", agent.velocity.magnitude);
        }
        else if (!moveTowards && agent.velocity.magnitude>0.0f)
        {
            calVelocity = agent.velocity.magnitude;
            calVelocity -= Time.deltaTime * deaccel;
            animator.SetFloat("Speed", calVelocity);
        }

        
        //CHECKS IF BOTH CONDITIONS HAVE MET AND RUNS THE WAYPOINT FOLLOWING CODE
        if (followWaypoint == true && (!agent.pathPending && agent.remainingDistance < 1.0f))
        {
            GotoNextPoint();
        }

        Debug.LogError("Bool" + followWaypoint);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            moveTowards = true;
            //DISABLES THE WAYPOINT FOLLOWING CODE TO RUN THE CHASE CODE INSTEAD
            followWaypoint = false;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            moveTowards = false; 
            //RE-ENABLES THE WAYPOINT FOLLOWING CODE ONCE THE PLAYER LEAVES THE TRIGGER AREA
            followWaypoint = true;
        }
    }

    //THIS IS THE WAYPOINT FOLLOWING CODE
    void GotoNextPoint()
    {
        animator.SetFloat("Speed", agent.velocity.magnitude);
        // Returns if no points have been set up
        if (points.Length == 0)
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;

        Debug.LogError("DestPoint = " + destPoint);
        // Choose the next point in the array as the destination.
        // cycling to the start if necessary.
        destPoint = Random.Range(0, points.Length);
    }
}

Hey there,

in general i cannot spot any obvious issue in your code but i guess we can still solve this.

First up some improvement suggestions. Since you have different behaviours that you switch between (patrol - followplayer) it is a nice thing to use enumerations here.

For exmaple like this:

  public enum AiState { Idle, Patrol, FollowPlayer }
  public AiState currentState = AiState.Idle;

Then you don’t need the 2 flags moveTowards and followPlayer anymore as you only have one state that you switch and check:

  if(currentState == AiState.FollowPlayer)
         //do player follow stuff

This way your code becomes more readable as you always know what is checked for and you will have less space for errors as there is only one variable to maintain.

Regarding your actual question i can only guess that the issue is somewhere in this check:

  (!agent.pathPending && agent.remainingDistance < 1.0f)

I am sadly not that familiar with the navmesh agent stuff so i am not sure if there is a particular better way to solve this with the agent itself but i guess that if you’d remove the check for agent.pathPending and instead added

 if(Vector3.distance(agent.distination, transform.position) > 1)
       return;

To your GoToNextPoint function this might fix the issue.

If not i’d suggest that you output what the values for remainingDistance and PathPending are so that you understand why this condition is never met.

I am also not sure if the fact that you randomize the next destination point after you set the destination is a good idea.
I’d suggest to switch this to this:

         // Choose the next point in the array as the destination.
         // cycling to the start if necessary.
         //WARNING: This does NOT choose the NEXT Point in the list but a random one. Can hit the same point twice.
         destPoint = Random.Range(0, points.Length);
         Debug.LogError("DestPoint = " + destPoint);
         // Set the agent to go to the currently selected destination.
         agent.destination = points[destPoint].position;

In addition i’d suggest to call this function in your Start so that the general behaviour is started with the function that you use to go to the next point.