How can I use a flag bool to decide if the character will keep moving i loop between waypoints ?

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

public class AgentControl : MonoBehaviour
{
    public List<Transform> points;
    public bool waitTimeToMove = false;
    //notice WaitTime is a float now
    public float WaitTime = 10f;
    public bool randomWaitTime;
    float waitMinTime = 1f;
    float waitMaxTime = 10f;
    public bool loop = false;

    private int destPoint = 0;
    private NavMeshAgent agent;
    private Transform originalPos;
    //two vars for handling timer
    private float timer = 0;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();

        // Disabling auto-braking allows for continuous movement
        // between points (ie, the agent doesn't slow down as it
        // approaches a destination point).
        agent.autoBraking = false;

        if (randomWaitTime == true)
        {
            WaitTime = Random.Range(waitMinTime, waitMaxTime);
        }

        //transforms dont exist without A GameObject and a GameObject doesn't exist without a transform
        //create a new GameObject to hold our position
        GameObject originalPositionObject = new GameObject();
        //set the new gameobjects position equal to where the transform is right now
        originalPositionObject.transform.position = transform.position;
        //add this to the points list instead
        points.Add(originalPositionObject.transform);
    }


    void GotoNextPoint()
    {
        // Returns if no points have been set up
        if (points.Count == 0)
            return;

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

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Count;
    }


    void Update()
    {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (!agent.pathPending && agent.remainingDistance < 1f)
        {
            //if wait to move is true
            if (waitTimeToMove)
            {
                //if timer is less than 10
                if (timer < WaitTime)
                {
                    //add Time.deltaTime each time we hit this point
                    timer += Time.deltaTime;
                }
                //no longer waiting because timer is greater than 10
                else
                {

                    waitTimeToMove = false;
                }
            }
            //if we hit here waitToMove is false, so go ahead as usual
            else
            {
                if (loop == false && destPoint != points.Count - 1)
                {
                    GotoNextPoint();
                }
            }
        }
    }
}

I tried to add this IF condition :

if (loop == false && destPoint != points.Count - 1)

but it’s not working. I want that if loop is false move between all the watpoints once and stop in the last waypoint. and if loop is true continue all the time to move between the waypoints.

Please describe specifically what “it’s not working” means.

1 Like

“it’s not working” means the loop part is not making the agents moving between the waypoints in a loop or not mode.
if loop is false they move once in the waypoints if it’s true they are not moving at all it’s all messed.

Try changing this line: (line 88)

if (loop == false && destPoint != points.Count - 1)

To this:

if (loop == true || destPoint != points.Count - 1)
1 Like

Why when the character is getting to the last waypoint and loop is false it’s not staying on the last waypoint without moving ? What it does is moving from side to side on the last waypoint on small area kind of loop but on small are on the last waypoint instead standing still on the last waypoint.