using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public List<Transform> objectsToMove;
public LineRenderer lineRenderer;
public float speed;
public bool go = false;
public bool moveToFirstPositionOnStart = false;
public float rotSpeed;
public bool random = false;
public int currentCurvedLinePointIndex;
private Vector3[] positions;
private Vector3[] pos;
private int index = 0;
private bool goForward = true;
private List<GameObject> curvedLinePoints = new List<GameObject>();
private int numofposbetweenpoints;
private bool getPositions = false;
int randomIndex;
int curvedPointsIndex;
public float delay;
private float[] startTimes;
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < objectsToMove.Count; i++)
{
var parent = GameObject.Find("Moving Object Parent");
objectsToMove[i] = Instantiate(objectsToMove[i], parent.transform);
objectsToMove[i].name = "Platfrom";
curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();
if (curvedLinePoints != null && curvedLinePoints.Count > 0)
{
objectsToMove[i].rotation = curvedLinePoints[1].transform.rotation;
}
}
startTimes = new float[objectsToMove.Count];
for (int i = 0; i < startTimes.Length; i++)
{
startTimes[i] = Time.time + (Random.value * delay);
}
}
Vector3[] GetLinePointsInWorldSpace()
{
positions = new Vector3[lineRenderer.positionCount];
//Get the positions which are shown in the inspector
lineRenderer.GetPositions(positions);
//the points returned are in world space
return positions;
}
// Update is called once per frame
void Update()
{
if (lineRenderer.positionCount > 0 && getPositions == false && CurvedLineRenderer.linesSet)
{
pos = GetLinePointsInWorldSpace();
numofposbetweenpoints = curvedLinePoints.Count;
if (moveToFirstPositionOnStart == true)
{
foreach (Transform objToMove in objectsToMove)
{
objToMove.position = pos[index];
}
}
getPositions = true;
}
if (go == true && lineRenderer.positionCount > 0)
{
Move();
}
}
int counter = 0;
int c = 1;
void Move()
{
for (int i = 0; i < objectsToMove.Count; i++)
{
if (Time.time < startTimes[i]) continue;
Vector3 newPos = objectsToMove[i].position;
float distanceToTravel = speed * Time.deltaTime;
bool stillTraveling = true;
while (stillTraveling)
{
Vector3 oldPos = newPos;
newPos = Vector3.MoveTowards(oldPos, pos[index], distanceToTravel);
distanceToTravel -= Vector3.Distance(newPos, oldPos);
if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok
{
// when you hit a waypoint:
if (goForward)
{
bool atLastOne = index >= pos.Length - 1;
if (!atLastOne)
{
index++;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else { index--; goForward = false; }
}
else
{ // going backwards:
bool atFirstOne = index <= 0;
if (!atFirstOne)
{
index--;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else { index++; goForward = true; }
}
}
else
{
stillTraveling = false;
}
}
objectsToMove[i].position = newPos;
}
}
}
For example, I have 3 objects to move. The first one start moving after 5 seconds and moving on the pos array positions.
Then the second object is start moving after 7 seconds but now instead moving also on the pos array positions the second object is reaching the first moving object and then move with him together.
The same thing happens with the last third object to move from the List objectsToMove.
I can't figure out why they are reaching the first object that move instead that each one will move on the pos array positions ?
Not sure if it's the delay line inside the Move function that make the problem :
if (Time.time < startTimes[i]) continue;
Or maybe the loop in the Move function :
for (int i = 0; i < objectsToMove.Count; i++)