Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 calebw2370 · Aug 05, 2020 at 01:14 AM · instantiatetransformwaitforsecondstranslateienumerator

How can I instantiate and move objects smoothly?

I am creating an endless runner game so I need there to be somewhere far up ahead there is an object with just a script that instantiates a road every 10 seconds, like this:

 IEnumerator InstantiateRoad()
 {
     while (true) {
         Instantiate(roadPrefab, new Vector3(transform.position.x, -0.25f, transform.position.z), Quaternion.identity);
         yield return new WaitForSecondsRealtime(timeBetween);
     }
 }

The roadPrefab shown above has a script on it that makes it move continuously like this:

 void Update()
 {
     transform.Translate(Vector3.back * Time.deltaTime * speed);
 }

However, this does not do the job smoothly since sometimes there is a very tiny space between blocks of road and sometime there is not a space. How can I fix this?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by VioKyma · Aug 05, 2020 at 02:52 AM

One thing to consider with your current approach is that the exact time a coroutine is run is not always consistent. Basically, if you say to Unity, WaitForSecondsRealtime(5), it's only going to run if those 5 seconds have lapsed, not if it gets close, so think of it more as "WaitForAtLeastSecondsRealtime()". That is to say, it will try to run as close to 5 seconds as possible, but it may be a little after that by the time it actually gets called.

One solution would be to determine where to place your next road (eg, the desired spacing between segments), and place them slightly in advance of when they're needed to give yourself some buffer.

As a side note: You might also want to look at object pooling, as Instantiate can be expensive when called in the middle of the game loop. Periodically removing old segments, putting them back in the pool, and then placing them at the end again can help keep things memory friendly and prevent stuttering/frame drops.

Comment
Add comment · Show 2 · 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 calebw2370 · Aug 05, 2020 at 09:31 PM 0
Share

Thank you for this helpful answer, I have one question. Would object pooling be necessary if only, say, 5 chunks of road will be active at once? I am developing for mobile so performance matters, but I'm not sure how much of a difference it will really make.

avatar image VioKyma calebw2370 · Aug 11, 2020 at 11:19 AM 0
Share

If you're on mobile, you're going to notice the Instantiate calls more during game play. Really, with performance it will depend on how much else is going on, as well as the devices you're targeting, but keep an eye on it. If it becomes a problem, you know where to look first :)

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

198 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 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 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

instantiate,destroy,gain speed in time 2 Answers

Can't move instantiated prefab 2 Answers

Instantiate an object as soon as another object is destroyed 2 Answers

Bullet Translation question 0 Answers

How do I re-instantiate an object after it is destroyed from the scene? 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