How to make multiple, evenly-spaced objects follow any path with any number of points?

Hello! Somewhat of a n00b here…

I have been trying for several hours to write a piece of code that allows several objects to follow a square path. As they travel, these objects should:

  • Be spaced evenly
  • Not overlap
  • Leave gaps of the same size in between them

Though I have objects that follow a path, these objects cluster at the corners before moving on, resulting in gaps and unevenness.

I’d greatly appreciate it if anyone has a solution! And thank you kindly for your time :slight_smile:

If you’re setting the object positions directly, you could try doing so from a single script that uses Vector3.Lerp(corner1, corner2, t). The advantage there is that you can have one t value that you subtract a fixed amount from to calculate the t values for the other objects, keeping them in lockstep. The following might work (I haven’t tested it.) corner0-3 would be the corners of the path, circuitTime is the number of seconds to go all the way around the path, timeSpacing is the number of seconds between objects, and objects is an array of the objects.

public GameObject corner0;
public GameObject corner1;
public GameObject corner2;
public GameObject corner3;

public float circuitTime;
public float timeSpacing;

public GameObject[] objects;

float tMain;

void Update()
{
    tMain += Time.deltaTime;
    if (tMain > circuitTime)
    {
        tMain -= circuitTime;
    }
    for (int i = 0; i < objects.Length; i++)
    {
        PositionObject(objects_, tMain - i * timeSpacing);_

}
}

void PositionObject(GameObject nextObject, float t)
{
Vector3 cornerA;
Vector3 cornerB;
float sideTime = circuitTime / 4f;
if (t < 0f)
{
t += circuitTime;
}
if (t < circuitTime * 0.25f)
{
cornerA = corner0.transform.position;
cornerB = corner1.transform.position;
}
else if (t < circuitTime * 0.5f)
{
cornerA = corner1.transform.position;
cornerB = corner2.transform.position;
t -= circuitTime * 0.25f;
}
else if (t < circuitTime * 0.75f)
{
cornerA = corner2.transform.position;
cornerB = corner3.transform.position;
t -= circuitTime * 0.5f;
}
else
{
cornerA = corner3.transform.position;
cornerB = corner0.transform.position;
t -= circuitTime * 0.75f;
}
t /= sideTime;
nextObject.transform.position = Vector3.Lerp(cornerA, cornerB, t);
}

Thanks so much to @ImpOfThePerverse - your script is genius!!

You saved my a lot of time and stress :slight_smile:

Also, I’m terribly sorry for the late reply - but with a lot of help, the script has also been modified so that it’ll work with any number of points with any amount of distance between them:

// Child objects of this GameObject describe the path to follow
	public GameObject path;

	// How long a single object should take to complete a circuit
	public float circuitTime;

	private List <GameObject> _objects;
	private List <Vector3> _points;
	private List <float> _lengths;
	private float _pathLength;
	private float _timeMain;

	private float AddSegment (Vector3 from, Vector3 to)
	{
		float length = Vector3.Distance (from, to);

		_lengths.Add (length);

		return length;
	}

	void Start ()
	{
		float pathLength = 0.0f;
		Vector3? lastPoint = null;

		_timeMain = 0.0f;

		_points = new List<Vector3> ();
		_lengths = new List<float> ();
		foreach (Transform child in path.transform) {
			Vector3 point = child.gameObject.transform.position;

			_points.Add (point);

			if (lastPoint != null) {
				pathLength += AddSegment (lastPoint.Value, point);
			}

			lastPoint = point;
		}
		pathLength += AddSegment (lastPoint.Value, _points [0]);
		_pathLength = pathLength;

		_objects = new List<GameObject> ();
		foreach (Transform child in transform) {
			_objects.Add (child.gameObject);
		}
	}

	void Update ()
	{
		float timeSpacing = circuitTime / _objects.Count;

		_timeMain += Time.deltaTime;
		if (_timeMain > circuitTime) {
			_timeMain -= circuitTime;
		}

		for (int i = 0; i < _objects.Count; i++) {
			if (_objects<em>!=null&&_objects *.GetComponent<SpriteRenderer>().enabled) {*</em>

PositionObject (objects , timeMain + i * timeSpacing);
* }*

* }*
* }*

* private int FindSegment (float distance)*
* {*
* int segment = 0;*
* foreach (float length in lengths) {*
* distance -= length;
if (distance < 0.0f) {
return segment;
}*_

* segment += 1;*
* }*

* // Should never reach here*
* return -1;*
* }*

* private float DistanceToSegment (int segment)*
* {*
* float total = 0.0f;*

* for (int i = 0; i < segment; i++)*
total += _lengths ;

* return total;*
* }*

* void PositionObject (GameObject gObject, float t)*
* {*
* int segment;*
* float distance, partialDistance;*
* Vector3 pointA, pointB;*

* if (t > circuitTime) {*
* t -= circuitTime;*
* }*

distance = pathLength * (t / circuitTime);
* segment = FindSegment (distance);_
pointA = points [segment];
if (segment < points.Count - 1) {
pointB = points [segment + 1];*
* } else {*

* pointB = points [0];*
* }*

* partialDistance = distance - DistanceToSegment (segment);*
* gObject.transform.position = Vector3.Lerp (pointA, pointB, partialDistance / lengths [segment]);*
* }*_