- Home /
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 :)
Answer by Leia · May 19, 2018 at 09:45 AM
Thanks so much to @ImpOfThePerverse - your script is genius!!
You saved my a lot of time and stress :)
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[i]!=null&&_objects [i].GetComponent<SpriteRenderer>().enabled) {
PositionObject (_objects [i], _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 [i];
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]);
}
Answer by ImpOfThePerverse · May 17, 2018 at 11:47 PM
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[i], 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);
}
Thank you so much - your script is genius!!
You saved my a lot of time and stress :)
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 (see my script below).
(Also, I apologise for the duplicated message and comment; it would appear that I am also a n00b at using Unity Forums)