Why won't my AI change waypoints for patroling?

Hello There!
I made a enemy AI script. In that AI script I have a patrol state. That patrol state does not work properly. The waypoints for the AI is an array of gameobjects. The AI will go to point 0, then it will start rotating crazily and it won’t move to the next waypoint. Here’s the Patrol part of the code:

 IEnumerator Patrol()
    {
        patrol = true;
        canSeePlayer = attacking = isSearching = false;
        waitTime = startWaitTime;
        amount = 0;
        agent = GetComponent<NavMeshAgent>();
        moveSpotsStop = 1;

        agent.destination = moveSpots[0].position;
        agent.speed = patrolSpeed;
        Vector3 moveSpotsPosition = new Vector3(moveSpots[amount].position.x,
                                         transform.position.y,
                                         moveSpots[amount].position.z);
        transform.LookAt(agent.destination);

        while (patrol == true)
        {

            if (Vector3.Distance(transform.position, agent.destination) <= moveSpotsStop)
            {
                arrivedAtPoint = true;
                amount += 1;

                agent.destination = moveSpots[amount].position;
                agent.speed = patrolSpeed;

                if (amount >= moveSpots.Length - 1)
                {
                    amount = 0;
                }
                

                
                transform.LookAt(agent.destination);

            }
            else
            {
                arrivedAtPoint = false;
            }


            if (canSeePlayer)
            {
                patrol = false;
                stateNumber = 3;
                break;
                
            }

            yield return null;
        }
        yield return new WaitForSeconds(0);
    }

The bool arrived at point is set to true when the AI arrives at the first point, but it won’t move to point 2.

Can anyone please help me? I’ve been at this problem for over 2 days with 15 hours. Help will be appriciated.
Thank you.

First of all transform.LookAt(agent.destination); is not a good idea to use when moving with navmeshagent. The agent already takes care of rotation by himself and what I think it happens is that two components try to modify rotation at the same time. That is maybe why you have a awkward rotation.

Secondly, the check of the amount being bigger than array (line 27) size should be put before you actually access the array index. It will be out of bounds if you keep it like this. And get a nice exception"System.IndexOutOfRangeException: Index was outside the bounds of the array"

Apart from that every thing should be ok. As advice you should not call GetComponent<> in a function if the function is called more times, just cache it in the Start() and Awake(), this also makes the code cleaner.

As for the implementation unity coroutines are cool, I have to admit. I usually prefer another way to handle states using a enum. patrol code. - Pastebin.com here is a snippet of my current work in progress for an ai. Also a pro tip about how to set the patrol points in a easy way to preview in scene view. patrol hosted at ImgBB — ImgBB unity gizmos is pretty cool right?
@intelligent_chicken

Good luck with you ai!. Cheers :slight_smile:

Best Regards,

Nick