I have trails for my agent to move to. The trails are meshes that are divided into sections. Every second, the trails are going to be destroyed from the first item on the list and so on, one by one. And my agent is going to move to the next trail section in the list.
For example: trail[0] is going to be destroyed, then trail[1] is going to be the next trail[0] after that.
In this script, my agent moves to the first trail location on the list. Then it's stuck and not moving anywhere, even the trails on the list are starting destroyed.
How to make my agent update the location of the trail on the list (In this case, after the first trail gets destroyed)?
Thank you in advance :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System;
using System.Linq;
public class MoveToTrail : MonoBehaviour
{
[SerializeField] GameObject[] trailTargets;
[SerializeField] List<GameObject> trailLists = new List<GameObject>();
public string tag;
public GameObject agents;
[SerializeField] private float t;
[SerializeField] private float speed;
private NavMeshAgent navMeshAgent;
private void Awake()
{
navMeshAgent = GetComponent<NavMeshAgent>();
navMeshAgent.speed = speed;
}
private void Start()
{
}
private void Update()
{
trailTargets = GameObject.FindGameObjectsWithTag(tag);
trailLists = trailTargets.ToList();
MoveToTarget();
}
void MoveToTarget()
{
for (int i = 0; i < trailLists.Count; i++)
{
Vector3 agentsPos = agents.transform.position;
Vector3 targetPos = trailLists[0].transform.position;
transform.position = Vector3.MoveTowards(agentsPos, Vector3.Lerp(agentsPos, targetPos, t), speed);
}
}
}