# Move objects in array with positions array

I am trying to get a line of spheres to move around but all i get is the first sphere to move and get the rest to follow as one instead of building a sort of a train of spheres. Any idea how to alter the code to get it right? In the code bellow i get the first pbject in listBalls to move nicely along the Bezier curve but the rest move as one and overlap with the first sphere too.

    void Update()
{

t += Time.deltaTime * 0.2f;
ballPosition = Mathf.Pow(1 - t, 3) * p0 + 3 * Mathf.Pow(1 - t, 2) * t * p1 + 3 * (1 - t) * Mathf.Pow(t, 2) * p2 + Mathf.Pow(t, 3) * p3;
listBalls.transform.position = positions[j];
for (i = 1; i < listBalls.Count; i++)
{
Transform ball = listBalls[i].transform;
Transform nextBall = listBalls[i - 1].transform;
if ((ball.position - nextBall.position).magnitude > 1f)
{
ball.position = positions[j];
}
}
j++;


Feels like it should be something easy, setting the N-1 balls to same position as the first ball must be wrong. I tried setting them at positions[j-2] but it didn't work either.

Edit: the Spheres should start rolling left to right one after the other eventually all 10 should be on the screen and when they reach P3 they should restarting the rolling.

• listBalls is the List with 10 spheres
• positions is the List with 50 Vector3 positions along the curve. [![enter code here]]

• It's not clear to me what kind of movement you want here. Can you edit your question to include a diagram to walk us through how the game should start, how it should look next frame, and a few frames after that? – DMGregory Aug 2 at 15:34

It looks like you want something like this:

t += Time.deltaTime * 0.2f;

ballPosition =     Mathf.Pow(1 - t, 3)                   * p0
+ 3 * Mathf.Pow(1 - t, 2) * t               * p1
+ 3 * (1 - t)             * Mathf.Pow(t, 2) * p2
+                           Mathf.Pow(t, 3) * p3;

// Put first ball at the last position in the list. It's our first "leader"
int positionIndex = positions.Count - 1;

for (i = 1; i < listBalls.Count; i++)
{
// Back up through the positions list until you find one that's 1.0 units
// or further behind our current "leader" ball.
do {
positionIndex--;
} while ( (positionIndex >= 0 )
&& ( (positions[positionIndex] - leaderPosition).sqrMagnitude < 1f*1f )
);

// If we exhausted our available positions, abort
// and leave the rest of the balls where they are.
if(positionIndex < 0) break;

// This position becomes our new leader for the next ball to follow.

// Position our current ball at this leader position.