how to make enemy go to points in array in order and not randomly?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class SWATwalking : StateMachineBehaviour

{
    float SWATwalkingtime;
    NavMeshAgent SWATagent01;

     private GameObject[] SWATwaypointsAC;

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        SWATwalkingtime = 0f;

        if(SWATwaypointsAC == null)
        {
            SWATwaypointsAC = animator.gameObject.GetComponent<SWATwaypoints>().SWATwaypoints01;
        }

        SWATagent01 = animator.GetComponent<NavMeshAgent>();

        SWATagent01.SetDestination(SWATwaypointsAC[Random.Range(0, SWATwaypointsAC.Length)].transform.position);
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        SWATwalkingtime += Time.deltaTime;

        if (SWATwalkingtime >= 5)
        {
            animator.SetBool("IsWalking", false);
        }
    }

 
}

line 26 is what i need to change, enemy patrols randomly between the four objects in the ray but i want the enemy to move between points in order eg 0, 1, 2, 3 and not randomly but idk how to set the range :slight_smile:

Just keep an int currentIndex and add 1 to it each time you need to go to the next waypoint. Wrap around to 0 when you reach the end (using % or an if statement)

1 Like

thank you!! :slight_smile: