Search Unity

Question Why the objects that are moving between the waypoints are moving to each other instead ?

Discussion in 'Navigation' started by shamenraze1988, Apr 20, 2021.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine;
    6.  
    7. public class Waypoints : MonoBehaviour
    8. {
    9.     public List<Transform> objectsToMove;
    10.     public LineRenderer lineRenderer;
    11.     public float speed;
    12.     public bool go = false;
    13.     public bool moveToFirstPositionOnStart = false;
    14.     public float rotSpeed;
    15.     public bool random = false;
    16.     public int currentCurvedLinePointIndex;
    17.  
    18.     private Vector3[] positions;
    19.     private Vector3[] pos;
    20.     private int index = 0;
    21.     private bool goForward = true;
    22.     private List<GameObject> curvedLinePoints = new List<GameObject>();
    23.     private int numofposbetweenpoints;
    24.     private bool getPositions = false;
    25.     int randomIndex;
    26.     int curvedPointsIndex;
    27.     public float delay;
    28.     private float[] startTimes;
    29.  
    30.     // Start is called before the first frame update
    31.     void Start()
    32.     {
    33.         for (int i = 0; i < objectsToMove.Count; i++)
    34.         {
    35.             var parent = GameObject.Find("Moving Object Parent");
    36.             objectsToMove[i] = Instantiate(objectsToMove[i], parent.transform);
    37.             objectsToMove[i].name = "Platfrom";
    38.  
    39.             curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();
    40.  
    41.             if (curvedLinePoints != null && curvedLinePoints.Count > 0)
    42.             {
    43.                 objectsToMove[i].rotation = curvedLinePoints[1].transform.rotation;
    44.             }
    45.         }
    46.  
    47.         startTimes = new float[objectsToMove.Count];
    48.  
    49.         for (int i = 0; i < startTimes.Length; i++)
    50.         {
    51.             startTimes[i] = Time.time + (Random.value * delay);
    52.         }
    53.     }
    54.  
    55.     Vector3[] GetLinePointsInWorldSpace()
    56.     {
    57.         positions = new Vector3[lineRenderer.positionCount];
    58.         //Get the positions which are shown in the inspector
    59.         lineRenderer.GetPositions(positions);
    60.  
    61.  
    62.         //the points returned are in world space
    63.         return positions;
    64.     }
    65.  
    66.     // Update is called once per frame
    67.     void Update()
    68.     {
    69.        
    70.  
    71.         if (lineRenderer.positionCount > 0 && getPositions == false && CurvedLineRenderer.linesSet)
    72.         {
    73.             pos = GetLinePointsInWorldSpace();
    74.             numofposbetweenpoints = curvedLinePoints.Count;
    75.  
    76.             if (moveToFirstPositionOnStart == true)
    77.             {
    78.                 foreach (Transform objToMove in objectsToMove)
    79.                 {
    80.                     objToMove.position = pos[index];
    81.                 }
    82.             }
    83.  
    84.             getPositions = true;
    85.         }
    86.  
    87.         if (go == true && lineRenderer.positionCount > 0)
    88.         {
    89.             Move();
    90.         }
    91.     }
    92.  
    93.     int counter = 0;
    94.     int c = 1;
    95.     void Move()
    96.     {
    97.         for (int i = 0; i < objectsToMove.Count; i++)
    98.         {
    99.             if (Time.time < startTimes[i]) continue;
    100.  
    101.             Vector3 newPos = objectsToMove[i].position;
    102.             float distanceToTravel = speed * Time.deltaTime;
    103.  
    104.             bool stillTraveling = true;
    105.             while (stillTraveling)
    106.             {
    107.                 Vector3 oldPos = newPos;
    108.  
    109.                 newPos = Vector3.MoveTowards(oldPos, pos[index], distanceToTravel);
    110.  
    111.                 distanceToTravel -= Vector3.Distance(newPos, oldPos);
    112.                 if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok
    113.                 {
    114.                     // when you hit a waypoint:
    115.                     if (goForward)
    116.                     {
    117.                         bool atLastOne = index >= pos.Length - 1;
    118.                         if (!atLastOne)
    119.                         {
    120.                             index++;
    121.                             counter++;
    122.                             if (counter == numofposbetweenpoints)
    123.                             {
    124.                                 c++;
    125.  
    126.                                 counter = 0;
    127.                             }
    128.                             if (c == curvedLinePoints.Count - 1)
    129.                             {
    130.                                 c = 0;
    131.                             }
    132.                         }
    133.                         else { index--; goForward = false; }
    134.                     }
    135.                     else
    136.                     { // going backwards:
    137.                         bool atFirstOne = index <= 0;
    138.                         if (!atFirstOne)
    139.                         {
    140.                             index--;
    141.  
    142.                             counter++;
    143.                             if (counter == numofposbetweenpoints)
    144.                             {
    145.                                 c++;
    146.  
    147.                                 counter = 0;
    148.                             }
    149.                             if (c == curvedLinePoints.Count - 1)
    150.                             {
    151.                                 c = 0;
    152.                             }
    153.                         }
    154.                         else { index++; goForward = true; }
    155.                     }
    156.                 }
    157.                 else
    158.                 {
    159.                     stillTraveling = false;
    160.                 }
    161.             }
    162.  
    163.             objectsToMove[i].position = newPos;
    164.         }
    165.     }
    166. }
    167.  
    The array pos contains over 5000 positions to move between.
    The variable List<Transform> objectsToMove was single Transform objToMove.

    I changed it to a List<Transform> and added a delay for some seconds so when the objectsToMove start moving between the waypoints they will not move at the same time in a group like one object.

    So when running the game each object to move in the List start moving after another few seconds.

    The problem is that the first object move on the waypoints but the other objects to move are moving to the first object to move and reaching him and then moving with him together instead that each object to move from the list will move on his own on the waypoints.

    I can't figure out why it's moving to each other instead moving over the waypoints.

    When it was with single object to move it was working fine but after adding the delay part and making loop in the Update over the objectsToMove the problem started.