using UnityEngine;
using System.Collections;

public class OffsetScroller : MonoBehaviour {

    public float scrolSpeed;

    Material mat;

    public bool move;

    public bool rightToLeft;

    void Awake()
    {
        move = true;
        mat = GetComponent<MeshRenderer>().material;
    }

    void Update()
    {
        if(move)
            Scrolling();
    }

    public void Scrolling()
    {
        if (rightToLeft)
        {
            float x = Mathf.Repeat(Time.time * scrolSpeed, 1);
            Vector2 offset = new Vector2(x, 0);
            mat.SetTextureOffset("_MainTex", offset);
        }
        else
        {
            float x = Mathf.Repeat(Time.time * -scrolSpeed, 1);
            Vector2 offset = new Vector2(x, 0);
            mat.SetTextureOffset("_MainTex", offset);
        }

    }

    void OnDisable()
    {
        mat.SetTextureOffset("_MainTex", Vector2.zero);
    }


    public bool Move
    {
        get
        {
            return move;
        }

        set
        {
            move = value;
        }
    }
}
