- Home /
Why Instantiated prefabs in continuous position sequence are overlapping on update??
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.
Am I doing it completely wrong and I should make parent gameobject with child as cars to let them move in a queue??? Any one? HELP!?
Answer by Bunny83 · Sep 03, 2017 at 10:11 AM
Well, this is a common problem that is related to how you "wrap" the objects position.
You check if an object has moved past a certain point on the left side. This is correct. However the problem is that you move the object to a fix position.
Since the amount the object has moved past that left point is different for each object, the gaps between the objects change randomly because you basically cut off this amount.
One solution is to not moving the object to a fix position but rather by a fix amount to the right. In your case you want to add screenSize.x + 6.0f to the current x position.
That will preserve the tiny offsets
Furthermore if your different car prefabs have different sizes, your instantiate code won't work properly. You offset each car based on the size of the current car. So you don't take the sizes of the previous cars into account.
Ins$$anonymous$$d of offsetting each car based on "I" you may want to use a Vector3 variable that holds the position of the last instantiated object and you just add the size of the current car each loop iteration.
However if the cars all have the same size it wouldn't matter. Though in this case it would be more readable if you read the car size once into a local variable before the loop
Your answer
Follow this Question
Related Questions
why do these both codes work similarly when they are put inside update function... 2 Answers
why do these both codes work similarly when they are put inside update function... 3 Answers
refactoring functions which *must* be in Update() 0 Answers
Update breaks function 2 Answers
Help with sound playing when a certain bool = false 1 Answer