using UnityEngine;

public class ScrollingBackground : MonoBehaviour
{

    private Transform[] background;
    private float backgroundWidth;
    public float ScrollSpeed = 0.2f;

    public void Start()
    {
        background = this.GetComponentsInChildren<Transform>();
        backgroundWidth = background[0].gameObject.GetComponentInChildren<Renderer>().bounds.size.x-0.02f;
        Vector3 pos = Vector3.right * -backgroundWidth * 0.5f;
        for (int i = 0; i < background.Length; i++)
        {
            if (background[i] != transform)
            {
                background[i].position = pos;
                pos += Vector3.right * backgroundWidth;
            }
        }
    }

    public void Update()
    {
        for (int i = 0; i < background.Length; i++)
        {
            if (background[i] != transform)
            {
                if (background[i].position.x < -backgroundWidth * 1.5)
                {
                    background[i].position = Vector3.right * backgroundWidth * 1.5f;
                }
                background[i].position += Vector3.left * ScrollSpeed * Time.deltaTime;
            }
        }

    }
}
