- Home /
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.
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));
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
![](https://koobas.hobune.stream/wayback/20220612201714im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Resolution Independent Scrolling Background 0 Answers
Perspective scrolling background 1 Answer