using UnityEngine;
using UnityEngine.AI;

public class SetNavTarget : MonoBehaviour
{
    public NavMeshAgent agent;
    public GameObject target;

    public bool trackTargetMovement = false;

    // Time in seconds between checks
    float targetCheckRate = 1f;
    float lastTime = 0;
    
    void Start()
    {
        // set the destination to the position of the target
        // the nav system handles all the pathfinding
        agent.SetDestination(target.transform.position);
    }

    void Update()
    {
        // every so often reset the destination in case the target has moved
        if(trackTargetMovement && Time.time > lastTime + targetCheckRate)
        {
            agent.SetDestination(target.transform.position);
            lastTime = Time.time;
        }
    }
}
