Applying two different translations at the same time to the same GameObject.

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.

So I assume You have BGZoom script attached to each background pice and then You tell them to zoom.

Bad idea. Instead create a parent object called Background or something on those lines. Make each object a child of that object and attach BGScroll and BGZoom script to the parent. So that parent is controlling movement of all the objects instead each of them controlling their own movement.

This will prevent Your bug that is happening because floats are not precise measure units. So what is happening is that one object end up in 3.65, another end up in 3.66, and another end up at 3.659 etc.

Thanks for the answer. No, BGZoom is attached to a parent object holding all the elements. Only BGScroll is attached to each object individually because many object are scrolling with different speed due to parallax effect.

For the records: what is the update order for a parent and its children? Parent first, then the children or children first, parent last? Maybe LateUpdate could do the trick…