using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public GameObject objToMove;
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()
{
var parent = GameObject.Find("Moving Object Parent");
objToMove = Instantiate(objToMove, parent.transform);
objToMove.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();
Move();
}
}
int counter = 0;
int c = 1;
void Move()
{
Vector3 newPos = objToMove.transform.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;
}
}
Now it's working fine with the single object the variable objToMove but I want to turn the variable either to a List or to Array like objsToMove and then using some timer or a coroutine to fire each X seconds for example every 3 seconds to start moving the next objToMove between the waypoints from the List/Array.