Hello folks,
Previously I did an infinite scrolling for a shmup game:
// BGScroller
void Start () {
startPos = new Vector3 (transform.position.x, height, transform.position.z);
}
// Update is called once per frame
void Update () {
Vector3 movement = new Vector3 (0, (scrollSpeed * -1), 0);
movement *= Time.deltaTime;
transform.Translate (movement);
if (transform.position.y < (height * -1))
{
transform.position = startPos;
}
}
Which works nice and fancy.
Now, I did another script which moves the background towards the camera to make it look bigger and give the illusion to the player that he is getting closer to the ground. Note: the background is composed of several objects like background, trees, shadows. Each of them are containing the BGScroller script, each with a different scrolling speed.
// BGZoomer
void Start () {
stateCooldown = 1.0f;
zoomed = false;
zooming = false;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("ChangeMode") == true && stateCooldown <= 0)
{
zooming = true;
stateCooldown = 3.0f;
}
if (zooming && zoomed)
{
Vector3 movement = new Vector3 (0, 0, zoomSpeed);
movement *= Time.deltaTime;
transform.Translate (movement);
if (transform.position.z >= 3.65f) // Z position hardcoded
{
zooming = false;
zoomed = false;
}
}
if (zooming && !zoomed)
{
Vector3 movement = new Vector3 (0, 0, (zoomSpeed *-1));
movement *= Time.deltaTime;
transform.Translate (movement);
if (transform.position.z <= 0.0f)
{
zooming = false;
zoomed = true;
}
}
if (stateCooldown > 0)
stateCooldown = stateCooldown - Time.deltaTime;
}
The problem is: at some point they desync. Like, the first group of trees get disconnected from the second group, or the shadows, or the first group looks bigger than the second and so on…
Looks like those two transform.Translate are not being applied at the same time. What am I doing wrong?
Thanks in advance.