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

public class SkyLooper : MonoBehaviour {
    public GameObject sky;
    public GameObject sky2;

    private float outOfView = -1100f;
    private bool backForth;
    private Vector3 skyOrig;
    private float speed;
    public void Start() {
        sky.transform.localPosition = new Vector3(5887f, 285f, 656.67f);
        sky2.transform.localPosition = new Vector3(2269f, 285f, 674f);
        skyOrig = sky.transform.localPosition;
        speed = 25f;
}
    void Update () {
        sky.transform.localPosition = new Vector3(sky.transform.localPosition.x - Time.deltaTime * speed, sky.transform.localPosition.y, sky.transform.localPosition.z);
        sky2.transform.localPosition = new Vector3(sky2.transform.localPosition.x - Time.deltaTime * speed, sky2.transform.localPosition.y, sky2.transform.localPosition.z);
        if (sky.transform.localPosition.x < outOfView){
           RepositionBackground(sky);
        }
        if(sky2.transform.localPosition.x < outOfView) {
            RepositionBackground(sky2);
        }
    }

   private void RepositionBackground(GameObject sky) {
        if(backForth) {
            backForth = !backForth;
            sky.transform.localPosition = skyOrig;
        } else {
            sky.transform.localPosition = new Vector3(skyOrig.x, skyOrig.y, skyOrig.z + 5f);

        }
    }
}
