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

public class GoToMultiDestinations : MonoBehaviour
{
    public GameObject[] goals;
    public bool randomGoal = false;
    public float goalDistance = 0.1f;

    NavMeshAgent agent;
    int currentGoal = 0;
    
    void Start()
    {
        // get the navmeshagent on this game object
        agent = GetComponent<NavMeshAgent>();

        SetNewGoal();   
    }

    void SetNewGoal()
    {
        // skip if there aren't any goals
        if (goals.Length == 0)
            return;

        // set goal using currentgoal
        var goal = goals[currentGoal];

        if (randomGoal)
            goal = goals[Random.Range(0, goals.Length)];

        agent.SetDestination(goal.transform.position);

        // increment the current goal and loop to beginning
        currentGoal++;
        if (currentGoal >= goals.Length)
            currentGoal = 0;
    }

    void Update()
    {
        // check distance from the destination
        // and set new goal if we're inside the goal distance
        if (agent.remainingDistance < goalDistance)
            SetNewGoal();
    }
}
