How to make the background follow the camera

I’m making a 2D infinite runner game, so now I wanted to add a background for the game, so I made a quad and attach the image as texture with offset looping so it will show the repeating effect.

here’s the script of the looping background wich works great.

using UnityEngine;

public class Background : MonoBehaviour {

    public float speed = 0.5f;
    void Update() {

        Vector2 offset = new Vector2(Time.time * speed, 0);
        GetComponent<Renderer>().material.mainTextureOffset = offset;
     }
}

Now the problem, my camera is an orthographic and made follows the Player buy through code so the camera view won’t jump when the Player jump, but I can’t make the background (AKA quad) follows the camera…!?? because the camera moves to positive x endlessly but the background is still…!

the most obvious way is by make the Background a child to the camera, but once I do that the background transform value reset as the Camera and it stopped showing…!?

The camera z position is -20 with (0,0,0) scale and the background has z position of 10 with (35,20,0) scale …!!

so How can I make the Background follow a moving camera endlessly without changing the original values of the z-position or the scale…!?

here’s a video that i recored which concise the problem…

If the Camera script is wanted, I’ll add
it…!

or you could parent the background to the camera

If your wanting a parallax effect this one works great just attach this to your camera. And add your backgrounds in order or you can add a Background GameObject with all of you backgrounds as child objects. Make sure to set the z coordinates of your background object before running. i set mine to -14.

using UnityEngine;
using System.Collections;

public class CameraParallax : MonoBehaviour {

public Transform[] backgrounds;
public float[] parallaxScales;
public float smoothing;

private Transform cam;

private Vector3 previousCamPos;

// Use this for initialization
void Start () {
	
	cam = Camera.main.transform;
	
	previousCamPos = cam.position;
	
	parallaxScales = new float[backgrounds.Length];
	
	
	for (int i = 0; i < backgrounds.Length; i++) {
		
		parallaxScales <em>= backgrounds_.position.z * -1;_</em>

_ Debug.Log(parallaxScales*);*_

* }*

* }*

* // Update is called once per frame*
* void FixedUpdate () {*

* for (int i = 0; i < backgrounds.Length; i++) {*

_ float parallax = (previousCamPos.x - cam.position.x) * parallaxScales*;*_

_ float backgroundTargetPosX = backgrounds*.position.x + parallax;*_

Vector3 backgroundTargetPos = new Vector3 (backgroundTargetPosX, backgrounds_.position.y, backgrounds*.position.z);*_

backgrounds.position = Vector3.Lerp (backgrounds_.position, backgroundTargetPos, smoothing * Time.deltaTime);_

* }*

* previousCamPos = cam.position;*

* }*
}

The easiest way would be to subtract the camera’s location X from your offset x in your update code.

Something like:

transform.position = new Vector3(camera.transform.x, transform.position.y, transform.position.z);
Vector2 offset = new Vector2(Time.time * speed - camera.transform.x, 0);

I created this code myself to follow the camera x but here’s the changed edition:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class folCam : MonoBehaviour {

    private float camerax;
    private float cameray;

    // Use this for initialization
    void Start () {
        
	}
	
	// Update is called once per frame
	void LateUpdate () {
        camerax = Camera.main.transform.position.x;
        cameray = Camera.main.transform.position.y;
        transform.position = new Vector3(camerax, cameray, transform.position.z);
	}
}

also this is on the background not the camera.

I get this error with the code:
IndexOutOfRangeException: Index was outside the bounds of the array.

The error is on this line:
float parallax = (cam.position.x - previousCamPos.x) * parallaxedScales*;*
@SBrooks75