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.