Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Move Agent to Next Selected Target and Repeat Sequence.

Discussion in 'Scripting' started by Holocene, Mar 16, 2021.

  1. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Hi all,
    I'm using the code below to move my agent to waypoints in a list.
    Currently my agent moves to the first selected target and remains there.
    What I'd like to do is modify this so my agent will move through the sequence of waypoints, repeating the selected target list infinitely.




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class AI_FindObjects : MonoBehaviour {
    5.      public List <Transform> Waypoints;
    6.      public Transform SelectedTarget;
    7.      void Update ()
    8.      {
    9.          SelectedTarget = null;
    10.          Enemies = new List<Transform>();
    11.          AddWaypointsToList();
    12.  
    13. }
    14.      public void AddWaypointsToList()
    15.      {
    16.          GameObject[] ItemsInList = GameObject.FindGameObjectsWithTag("Waypoint");
    17.          foreach(GameObject _Enemy in ItemsInList)
    18.          {
    19.              AddTarget(_Waypoint.transform);
    20.          }
    21.      }
    22.      public void AddTarget(Transform waypoint)
    23.      {
    24.          Waypoints.Add(waypoint);
    25.      }
    26.      public void DistanceToTarget()
    27.      {
    28.          Waypoints.Sort(delegate( Transform t1, Transform t2){
    29.              return Vector3.Distance(t1.transform.position,transform.position).CompareTo(Vector3.Distance(t2.transform.position,transform.position));
    30.          });
    31.      }
    32.      public void TargetedWaypoint()
    33.      {
    34.          if(SelectedTarget == null)
    35.          {
    36.              DistanceToTarget();
    37.              SelectedTarget = Waypoints[0];
    38.          }
    39.      }
    40.      void LateUpdate ()
    41.      {
    42.          TargetedWaypoint();
    43.          float dist = Vector3.Distance(SelectedTarget.transform.position,transform.position);
    44.           transform.position = Vector3.Lerp (transform.position, SelectedTarget.position, Time.deltaTime*1);
    45.      }
    46. }
    47.  
     
    Last edited: Mar 16, 2021
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    For starters, you're creating a whole new waypoints list every single frame at the bottom of Update, so that's not good.

    Instead, create a Queue in OnEnable(). When your script first starts, pop the first entry off the queue as your current target. When the agent is within an acceptable distance to the target, pop the next one off the queue and set that as the current target. Repeat until the queue is empty and then decide what's next for your agent (Rebuild the queue? Stop for a while? Up to you).
     
    Holocene likes this.
  3. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Thanks very much, GroZZleR!
    Between your reply here and another post which you had assisted (from 6 years ago), I’m all set.
    Appreciate your help across the years.
     
    GroZZleR likes this.