Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HerculeanInnovations · Sep 02, 2017 at 02:25 PM · prefabupdate functionupdate problemoverlappingsequence

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HerculeanInnovations · Sep 03, 2017 at 07:50 AM 0
Share

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!?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Sep 03, 2017 at 11:07 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges