using UnityEngine;
using System.Collections;

public class ScrollingLevelCamera : MonoBehaviour
{
    public bool scrollX = true;
    public bool scrollY = false;
    [Space]

    float characterOnScreen;
    public float z;

    public float farLeft = 0;

    [Space]

    public CharacterUserInput character;

    void Start()
    {
        z = transform.position.z;
    }

    void FixedUpdate()
    {
        if(scrollX)
            UpdateX();
        if(scrollY)
            UpdateY();
    }

    void UpdateX()
    {
        characterOnScreen = Camera.main.WorldToScreenPoint(character.transform.position).x;

        if (characterOnScreen > ((Screen.width / 2) + (Screen.width / 6)))
        {
            transform.position = Vector3.Lerp(transform.position, new Vector3(character.transform.position.x, transform.position.y, z), 1f * Time.deltaTime);
        }
        if (characterOnScreen < ((Screen.width / 2) - (Screen.width / 6)) && transform.position.x > 0)
        {
            transform.position = Vector3.Lerp(transform.position, new Vector3(character.transform.position.x, transform.position.y, z), 1f * Time.deltaTime);
        }
    }

    void UpdateY()
    {
        characterOnScreen = Camera.main.WorldToScreenPoint(character.transform.position).y;

        if (characterOnScreen > ((Screen.height / 2) + (Screen.height / 6)))
        {
            transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, character.transform.position.y, z), 1f * Time.deltaTime);
        }
        if (characterOnScreen < ((Screen.height / 2) - (Screen.height / 6)) && transform.position.y > 0)
        {
            transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, character.transform.position.y, z), 1f * Time.deltaTime);
        }
    }
}