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

public class BackgroundScroller : MonoBehaviour 
{	
	private RectTransform rectTransform;

	public float speed = 5;

	// Use this for initialization
	void Start () 
	{
		rectTransform = GetComponent<RectTransform>();
	}
	
	// Update is called once per frame
	void Update () 
	{
		var screenHeight = (float)Screen.height;
		var screenWidth = (float)Screen.width;

		var widthDelta = (rectTransform.rect.width - screenWidth  / 2);
		var q = widthDelta / speed;
		var widthTarget = Mathf.Sin(Time.time / q) * widthDelta;

		var heightDelta = rectTransform.rect.height - screenHeight;
		
		var x = widthTarget;
		var y = heightDelta / 2;

		var p = new Vector2(x, rectTransform.anchoredPosition.y);

		rectTransform.anchoredPosition = p;
	}
}
