(Unity6)一定時間ごとに追跡するターゲットを変更する方法

(サンプルコード)

using UnityEngine;
using UnityEngine.AI;

public class ChaseController : MonoBehaviour
{
    private GameObject[] players;
    private NavMeshAgent agent;

    private int num;
    private float count;
    private float interval = 5;

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

    void Update()
    {
        count += Time.deltaTime;

        if (count > interval)
        {
            players = GameObject.FindGameObjectsWithTag("Player");

            num = Random.Range(0, players.Length);

            agent.destination = players[num].transform.position;

            count = 0;
        }
    }
}

(実行結果)