• 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 khfrekek · Aug 04, 2016 at 05:36 AM · instantiateprefabspawnscrollinggaps

Spawned Scrolling background objects gaining a gap when speed increases.

Hi everyone!! I am making a game with an infinite background scroller that has an increasing speed. This background is made up of prefab blocks (with scroller spricpts attached) that are consistently instantiated to the right of the camera, such that their edges touch perfectly with no overlap or gap.

These blocks (all the same width) are spawned using a script that spawns a new block whenever the previous block has moved by the width of the block, using the following script:

 void Start()
     {
         lastPosition = meter.transform.position;
     }
 
     void Update()
     {
         meterDistance += Vector3.Distance(meter.transform.position, lastPosition);
         lastPosition = meter.transform.position;
         if (meterDistance >= 2.9f) //width of the prefab blocks
         {
             meterDistance = 0f;  //reset the "distance meter"
             Spawn(); //spawn a new background object
         }
     }

and these prefab blocks are using a scroller script as follows:

 void FixedUpdate()
     {
         //Scrolls the background (and obstacles) backwards by the scrollspeed. destroys when it gets too far
         newPosition = transform.position;
         newPosition.x -= PlayerController.scrollspeed;
         transform.position = newPosition;
         if (transform.position.x <= -20f) Destroy(this.gameObject);
     }

And this actually works perfectly; at least when the game begins at a constant scrollspeed.

However, when the scrollspeed increases, the objects start spawning in a lagged fashion: creating gaps in between every block of width proportional to the scrollspeed.

This makes no sense to me, because the spawn time is determined by the DISTANCE traveled, which is definitely proportional to the scrollspeed of that object. So does anyone have any idea why gaps would be appearing?

Thank you so much for your time and help! Zack

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me 1 Answer

How to Make an Expanding Spawn Area for Prefabs? 2 Answers

Error: Instantiated Enemies don't get hit 2 Answers

Spawning unique prefabs at different transform posititions 0 Answers

instatiates at a bad position 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges