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

public class SkyMoving : MonoBehaviour
{
    public GameObject sky1;
    public GameObject sky2;
    public GameObject sky3;
    public GameObject sky4;
    public GameObject sky5;
    public GameObject sky6;
    public GameObject sky7;
    public float speed;
    public float distance;
    public float y;
    public float Z;
    public Transform initPos;
    public Transform endPos;

    private void Start()
    {
        sky7.transform.position = endPos.position;
        sky6.transform.position = new Vector3(sky7.transform.position.x + distance, y, Z);
        sky5.transform.position = new Vector3(sky6.transform.position.x + distance, y, Z);
        sky4.transform.position = new Vector3(sky5.transform.position.x + distance, y, Z);
        sky3.transform.position = new Vector3(sky4.transform.position.x + distance, y, Z);
        sky2.transform.position = new Vector3(sky3.transform.position.x + distance, y, Z);
        sky1.transform.position = new Vector3(sky2.transform.position.x + distance, y, Z);
    }


    void Update()
    {
        sky1.transform.position -= new Vector3(speed, 0f, 0f) * Time.deltaTime;
        sky2.transform.position -= new Vector3(speed, 0f, 0f) * Time.deltaTime;
        sky3.transform.position -= new Vector3(speed, 0f, 0f) * Time.deltaTime;
        sky4.transform.position -= new Vector3(speed, 0f, 0f) * Time.deltaTime;
        sky5.transform.position -= new Vector3(speed, 0f, 0f) * Time.deltaTime;
        sky6.transform.position -= new Vector3(speed, 0f, 0f) * Time.deltaTime;
        sky7.transform.position -= new Vector3(speed, 0f, 0f) * Time.deltaTime;

        if (sky1.transform.position.x <= endPos.position.x)
        {
            sky1.transform.position = new Vector3(sky2.transform.position.x + distance, y, Z);
        }
        if (sky2.transform.position.x <= endPos.position.x)
        {
            sky2.transform.position = new Vector3(sky3.transform.position.x + distance, y, Z);
        }
        if (sky3.transform.position.x <= endPos.position.x)
        {
            sky3.transform.position = new Vector3(sky4.transform.position.x + distance, y, Z);
        }
        if (sky4.transform.position.x <= endPos.position.x)
        {
            sky4.transform.position = new Vector3(sky5.transform.position.x + distance, y, Z);
        }
        if (sky5.transform.position.x <= endPos.position.x)
        {
            sky5.transform.position = new Vector3(sky6.transform.position.x + distance, y, Z);
        }
        if (sky6.transform.position.x <= endPos.position.x)
        {
            sky6.transform.position = new Vector3(sky7.transform.position.x + distance, y, Z);
        }
        if (sky7.transform.position.x <= endPos.position.x)
        {
            sky7.transform.position = new Vector3(sky1.transform.position.x + distance, y, Z);
        }

    }
}
