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 K-Anator · May 19, 2019 at 07:51 PM · timebackgroundscrolling

Infinite scrolling background jumps when changing speed

So I have a 2D infinite scroller, it has a parallax background made up of 3 layers, each are quads. the scroll speed is meant to be based off of a scriptable float.

SceneSpeed.speed is set to 0.5f at the beginning of the game, every 30 seconds the game is running, the speed is increased by 0.5f.

Everything seems to work properly, at least until the first increase in speed is made. Then the backgrounds skip a lot, and sync up.

The BG scroller script:

 using UnityEngine;
 
 public class BGscroller : MonoBehaviour
 {
     public static BGscroller scrollerCon;
 
     public SceneSpeed SceneSpeed;
  
     public Renderer background1;    
     public Renderer background2;    
     public Renderer background3;
     
     void Awake()
     {
         if (scrollerCon == null)
         {
             DontDestroyOnLoad(gameObject);
             scrollerCon = this;
         }
 
         else if (scrollerCon != this)
         {
             Destroy(gameObject);
         }
     }
 
     void FixedUpdate()
     {
 
         //bg1
         float offset1 = (SceneSpeed.speed * Time.time) / 2;
         background1.material.SetTextureOffset("_MainTex", new Vector2(0, offset1));
         //bg2
         float offset2 = (SceneSpeed.speed * Time.time) / 4;
         background2.material.SetTextureOffset("_MainTex", new Vector2(0, offset2));
         //bg3
         float offset3 = (SceneSpeed.speed * Time.time) / 8;
         background3.material.SetTextureOffset("_MainTex", new Vector2(0, offset3));
     }
 }
  

The code responsible for increasing the speed:

     void Update()
     {
         // Speed up every X seconds
         time += Time.deltaTime;
         if (time >= timeStep)
         {
             //Debug.Log("Add " + timeStepAmount + " to sceneSpeed.");
             time = 0f;
             StartCoroutine(AddSpeed());
         }
 
 
     }
 
     IEnumerator AddSpeed()
     {
 
         for (float f = 1f; f >= 0; f -= 0.1f)
         {
             SceneSpeed.speed += timeStepAmount / 10;
             yield return new WaitForSeconds(0.1f);
         }
 
     }


I just don't understand what isn't working here. The scroller works fine with whatever speed I set it to, and the speed increases smoothly when I call AddSpeed(). But everything breaks the moment SceneSpeed is changed.

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
Best Answer

Answer by Zaeran · May 19, 2019 at 08:27 PM

It's possible that the skipping occurs because you're referencing Time.time, which is the total time elapsed since the start of the scene.

With offset 1 for example, at 30s when speed is 0.5, the offset is

 (0.5 * 30) / 2 = 7.5.

Once we jump to a speed of 1, the offset is

 (1.0 * 30) / 2 = 15

So right away we're jumping the offset by 7.5 instantly.

Instead, I'd have the offset add to the previous one, doing something like this:

 float offsetAddition = (SceneSpeed.speed * Time.fixedDeltaTime) / 2;
 float existingOffset = background1.material.GetTextureOffset("_MainTex").y;
 background1.material.SetTextureOffset("_MainTex", new Vector2(0, offsetAddition + existingOffset));

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 K-Anator · May 19, 2019 at 08:54 PM 0
Share

That worked perfectly, thank you! Apparently I still have some learning to do. I was originally trying to use Time.fixedDeltaTime as I do on an obstacle mover transform.Translate(Vector2.down * SceneSpeed.speed * Time.fixedDeltaTime); but that resulted in a whole lot of nothing moving.

I don't understand why I can directly multiply the vector.down of an object and have it work out just fine, but when it came to the texture, I have to do that addition.

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

106 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

Related Questions

Resolution Independent Scrolling Background 0 Answers

How can I make smooth scrolling background and obstacles ? Because my backgrounds and obstacles have shaking(vibration) when it play ? 0 Answers

How do I make the scrolling texture background change direction depending on keyboard input for the player movement controls? 3 Answers

Perspective scrolling background 1 Answer

Parallax scrolling background performance problem 4 Answers


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