Looping Between Waypoints

While putting together my own AI mostly from scratch, I’ve hit a bump in the patrol state I have setup. After reaching one waypoint, my AI won’t continue to the next one, despite being quite sure that my conditions to set the next point trigger and that I set the next one properly. I’ll copy the piece of code in question below:

public override void Update(enemyAI_FSM enemy)
    {
        // Check how close the enemy is to the player
        if (enemy.distance <= enemy.lookRadius)
        {
            enemy.TransitionToState(enemy.ShootState);
        }

        // If the Waypoint Index is set to less than or equal to the total number of waypoints, start moving toward the currently selected waypoint
        if (waypointIdx <= enemy.waypointList.Length && pointDistance > enemy.stopDistance)
        {
            enemy.transform.position = Vector3.MoveTowards(enemy.transform.position, currentPoint.transform.position, enemy.moveSpeed * Time.deltaTime);
        }
        // Then check if the waypoint has been reached, and select the next one
        else if (pointDistance <= enemy.stopDistance)
        {
            Debug.Log("Waypoint reached");
            if (waypointIdx < enemy.waypointList.Length)
            {
                waypointIdx++;
            }
            else if (waypointIdx >= enemy.waypointList.Length)
            {
                waypointIdx = 0;
            }
            currentPoint = enemy.waypointList[waypointIdx];
        }
    }

So, is there something obvious I’m missing? Thank you to anyone who can help

I don’t know what the pointDistance is? But most probably it was the issue as pointDistance > enemy.stopDistance is always set to be true in that if condition. The else if part never gets executed.
I copy-pasted your code in unity and modified it a bit (because I didn’t have all the variables and it’s values)

public class WaypointScript : MonoBehaviour
{
    private int waypointIdx = 0;
    public Transform[] waypointList;
    private float stopDistance = 3, moveSpeed = 10;   //i am assuming stopDistance is the distance from your waypoint where the waypointIdx should update
    private Transform currentPoint;

  private void Start()
    {
        currentPoint = waypointList[0];
    }

    private void Update()
    {
        if (waypointIdx <= waypointList.Length)
        {
            transform.position = Vector3.MoveTowards(transform.position, currentPoint.transform.position, moveSpeed * Time.deltaTime);
            if (Vector3.Distance(transform.position, waypointList[waypointIdx].transform.position) <= stopDistance)
            {
                Debug.Log("Waypoint reached");
                Debug.Log(waypointIdx);
                if (waypointIdx < waypointList.Length - 1)
                {
                    waypointIdx++;
                }
                else if (waypointIdx >= waypointList.Length - 1)
                {
                    waypointIdx = 0;
                }

                currentPoint = waypointList[waypointIdx];
            }
        }

    }
}

I Tested the code. It works like a charm. I hope it helps a bit.

Keep learning :slight_smile: