Hello, I am trying(in C#) to generate sequence of cars similar to cars in queue due to traffic jam.
So with “CarGenerate” prefab having 8 different car sprites. Different cars sprite are instantiated in sequence and are moved from right side of the screen to left side of the screen. And a private List UpdateCarList is also maintained .
Code to instantiate:-
for (int i = 0; i < MaxCars; ++i)//Max cars can be 15 in total
{
int idx = Random.Range(0, CarsSprite.Length);
Sprite currentCar = CarsSprite[idx];
GameObject newCar = Instantiate(CarsPrefab, new Vector3((screenSize.x + (currentCar.bounds.size.x * i)), (screenSize.y / 4 + 1.7f), -2.0f), transform.rotation);
UpdateCarList.Add(newCar);
}
and for moving the car from left to right ,transform.Translate is used with Mathf.repeat of velocity and delta time in update function.
Now when 1st car to last car in the sequence reaches the screen from right to left , Sequence cars position are updated respectively .But after running unity for some time I can see cars are overlapping.May be because of inappropriate position of the prefabs float values.So I unable to create a proper sequence of cars.
in update() function
foreach (GameObject go in UpdateCarList)
{
if (go.transform.localPosition.x <= -(screenSize.x + 5.0f))
{
go.transform.position = new Vector3((screenSize.x + 1.0f), screenSize.y / 4, -2.0f)
}
}
Requesting to help generate a proper sequence of cars like long queue in traffic jam. Well right now cars are only 15 but i think i will generate some 80 cars for my game.