How would i move every sphere that gets spawned every 2 seconds ? Basically i want every sphere to follow the previous spawned sphere or even all N-1 spheres should follow the first spawned sphere along a predefined path and they should keep rolling as a train of spheres. I am lost when i try to imprint movement to each sphere.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject[] prefabs;
public List<GameObject> listBalls = new List<GameObject>();
private GameObject x;
public int timer = 0;
private float movementSpeed = 5f;
void Start()
{
createBall();
}
void Update()
{
if (Time.time - timer > 0.8f)
{
createBall();
timer += 2;
}
listBalls[listBalls.Count-1].transform.position += new Vector3(0, 0, movementSpeed * Time.deltaTime);
}
void createBall()
{
x = prefabs[Random.Range(0, prefabs.Length)];
Instantiate(x, x.transform.position, Quaternion.identity);
listBalls.Add(x);
}
void moveBall(GameObject x, GameObject y)
{
x.transform.position += y.transform.position - new Vector3(0,0,-1);
}
}
Loop added but movement staggered
void Update()
{
if (Time.time - timer > 0.8f)
{
createBall();
timer += 2;
}
foreach (GameObject item in listBalls)
{
item.transform.position += new Vector3(0, 0, movementSpeed * Time.deltaTime);
}
}
```