using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movements : MonoBehaviour
{
public List<Transform> objectsToMove = new List<Transform>();
public List<Vector3> positions = new List<Vector3>();
public int amountOfPositions = 30;
public int minRandRange, maxRandRange;
public bool randomPositions = false;
public bool generateNewPositions = false;
public float duration = 5f;
public bool pingPong = false;
private void Start()
{
if (generateNewPositions || (positions.Count == 0 && amountOfPositions > 0))
GeneratePositions();
StartCoroutine(MoveBetweenPositions(duration));
}
private void GeneratePositions()
{
for(int i = 0; i < amountOfPositions; i++)
{
if (randomPositions)
{
var randPosX = UnityEngine.Random.Range(minRandRange, maxRandRange);
var randPosY = UnityEngine.Random.Range(minRandRange, maxRandRange);
var randPosZ = UnityEngine.Random.Range(minRandRange, maxRandRange);
positions.Add(new Vector3(randPosX, randPosY, randPosZ));
}
else
{
positions.Add(new Vector3(i, i, i));
}
}
}
IEnumerator MoveBetweenPositions(float duration)
{
for(int i = 0; i < positions.Count; i++)
{
float time = 0;
Vector3 startPosition = objectsToMove[0].position;
while(time < duration)
{
transform.position = Vector3.Lerp(startPosition, positions[i], time / duration);
time += Time.deltaTime;
yield return null;
}
objectsToMove[0].position = positions[i];
}
}
}
Now I have one object to move in the editor a cube. but if I have for example 20 cubes and I want that the first cube will move to the first position(waypoint) then after duration of 5 or any duration the next cube will move to the first position then the first cube will move to the second position and son all the cubes will move to the next position/s with speed and duration.
So each 5 seconds another cube start to move between the positions and each cube will move smooth with speed value between the positions.
For example the duration is 5 and also the speed is 5.
Later I want to use enum or something like that to be able to choose modes for example ping pong or backward or stop(each cube will stop in the last waypoint) or continue so the cubes will continue move between the waypoints nonstop.