Parallax + instantiated background

hello!
I am creating an infinite runner.
I have 3 different backgrounds ( background, middle ground and foreground ) for parallax scrolling.
And i also instantiate a new background when the camera is almost reaching the end of the background
so it goes on infinite.
But now i want to implement those things together and i don’t know how.
My parallax script:

using UnityEngine;
using System.Collections;

public class Parallax : MonoBehaviour {

    public Transform[] backgrounds;
    private float[] parralaxScales;
    public float smoothing = 1f;

    private Transform cam;
    private Vector3 previousCamPos;

    void Awake()
    {
        cam = Camera.main.transform;
    }
    // Use this for initialization
    void Start () {
        previousCamPos = cam.position;

        parralaxScales = new float[backgrounds.Length];

        for (int i = 0; i < backgrounds.Length; i++)
        {
            parralaxScales[i] = backgrounds[i].position.z * -1;
        }
    }
   
    // Update is called once per frame
    void Update () {
   
        for (int i = 0; i <backgrounds.Length; i++)
        {
            float parallax = (previousCamPos.x - cam.position.x) * parralaxScales[i];

            float backgroundTargetPosX = backgrounds[i].position.x + parallax;

            Vector3 backgroundTargetPos = new Vector3(backgroundTargetPosX, backgrounds[i].position.y, backgrounds[i].position.z);

            backgrounds[i].position = Vector3.Lerp(backgrounds[i].position, backgroundTargetPos, smoothing * Time.deltaTime);
        }

        previousCamPos = cam.position;
    }
}

The problem also is, for the backgrounds i use sprites, so i can’t change the Z axis in script however i can drag the object further away in the Z axis in Scene view, but the transform numbers don’t actually change, while in the scene view the background goes further away, and i need to do this for my parallax script. If i don’t pull it back, it doesn’t work.

But since i can’t change the Z axis from a script, how do i implement those 2 together.

Greetings,
Timo

ok, after looking at this, the real problem revolves that you are using the negative z to calculate the amount of movement you are trying to move that layer of background.

Say you have -1, -2 and -3 for backgrounds. You are moving 1 at (x * 1) and 2 at (x * 2) and 3 (x * 3) So your backgrounds farthest away get the most movement, the exact opposite of what you are trying to accomplish.

So, give yourself a max Z distance and use addition from the current z. (backgrounds_.position.z + maxDistance) This will give you the reverse numbers you need. So in this case, my max would be 4. 4 -1 is 3, so 1 would be (x * 3), 2 would be (x * 2) and 3 would be (x * 1)._
This should work in 2d and 3d.
```
*using UnityEngine;
using System.Collections;

public class Parallax : MonoBehaviour
{

public Transform[] backgrounds;
public float smoothing = 1f;
public float maxDistance;

private Transform cam;
private Vector3 previousCamPos;

void Awake()
{
    // get the main camera.
    cam = Camera.main.transform;
}
// Use this for initialization
void Start()
{
    if (cam == null) return;
    // captuer the current camera position
    previousCamPos = cam.position;
}

// Call this in the late update, to make sure it is the last thing to happen.
void LateUpdate()
{
    if (cam == null) return;
    foreach(Transform background in backgrounds)
    {
        // distance ratio of the current background to the max plane
        float z = maxDistance + background.transform.z;
        // the amount the camera has moved.
        float x = previousCamPos.x - cam.position.x;
        // the amount to move the background
        float amount = x * z;
        // get the target and move the x according to the camera.
        float target = background.position;
        target.x += amount;

        // you shouldnt be smoothing this result...
        background.position = Vector3.Lerp(background.position, target, smoothing * Time.deltaTime);
        //background.position = target;
    }
    // capture the current camera position
    previousCamPos = cam.position;
}

}*
```

Thanks for the help!
but somehow changing my camera’s projection fixed the problem of not seeing the numbers change in the Inspector.

but now the part of where when the background reaches the far left of the camera so it’s just out of it’s view
i need it to be placed on the right of the camera, also just outside of his view.
( Both the character and the camera are moving )
i have it kinda working with this script:

using UnityEngine;
using System.Collections;

public class Continue : MonoBehaviour {
    private Camera cam;
    public Transform target;
    private Vector3 positionVector;
    private float PosVectorXAxis;
    // Use this for initialization
    void Start () {
        cam = Camera.main;
    }
   
    // Update is called once per frame
    void Update () {
        Vector3 viewPos = cam.WorldToViewportPoint(transform.position);
        PosVectorXAxis = 768.1f + viewPos.x; // i tried this to calculate the X pos of where is needs to be placed
        positionVector = new Vector3(PosVectorXAxis, 39f, 2.5f); // this is where the background should be placed  once he is too far to the left
       
        //transform.Translate(Vector3.left * 5);
        if (viewPos.x < 0f)
        {
            Debug.Log("i am to the left of the Camera");
            transform.position = positionVector;
        }
        if(viewPos.x > 1.0)
        {
            Debug.Log("I am to the right of the camera");
        }
    }

    void FixedUpdate()
    {

    }
}

but it still bugs, because when the background of only half out of the screen it’s placed to a steady position on the right, so not relative to the camera.