What I’m trying to do is that each 3 second another object from a List will start moving between the waypoints.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public List<Transform> objsToMove = new List<Transform>();
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[] pos;
private int index = 0;
private bool goForward = true;
private List<GameObject> curvedLinePoints = new List<GameObject>();
private int numofposbetweenpoints;
private bool getPositions = false;
// Start is called before the first frame update
void Start()
{
for(int i = 0; i < objsToMove.Count; i++)
{
var parent = GameObject.Find("Moving Object Parent");
objsToMove[i] = Instantiate(objsToMove[i], parent.transform);
objsToMove[i].name = "Platfrom";
}
curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();
if (curvedLinePoints != null && curvedLinePoints.Count > 0)
{
//objToMove.transform.rotation = curvedLinePoints[1].transform.rotation;
}
}
Vector3[] GetLinePointsInWorldSpace()
{
var 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)
{
//objToMove.transform.position = pos[index];
}
getPositions = true;
}
if (go == true && lineRenderer.positionCount > 0 && CurvedLineRenderer.linesSet)
{
pos = GetLinePointsInWorldSpace();
StartCoroutine(SendObjToMove());
Move(objToMove);
}
}
int cc = 0;
private IEnumerator SendObjToMove()
{
yield return new WaitForSeconds(3f);
}
int counter = 0;
int c = 1;
void Move(Transform objToMove)
{
Vector3 newPos = objToMove.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;
}
}
objToMove.transform.position = newPos;
}
}
The part is :
if (go == true && lineRenderer.positionCount > 0 && CurvedLineRenderer.linesSet)
{
pos = GetLinePointsInWorldSpace();
StartCoroutine(SendObjToMove());
Move(objToMove);
}
Then :
int cc = 0;
private IEnumerator SendObjToMove()
{
yield return new WaitForSeconds(3f);
}
The function Move should be called in the Update to make the objects to move.
Then I thought to use a coroutine for the 3 seconds delay. and using the variable cc for index of the objects to send each 3 seconds.
Before all that with the coroutine I just did :
foreach (Transform objToMove in objsToMove)
{
Move(objToMove);
}
but that just send all the objects at the same time between the waypoints and I want to make a delay of X seconds between each object in the List objsToMove.