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

public class CameraControls : MonoBehaviour {

    public float camRotationTemp = 0;
    public float maxLeft = -30;
    public float maxRight = 30;

    Vector3 mouseStart;
    Vector3 mouseOffset;
    Vector3 rotateTemp = new Vector3();
    public float turnSpeed = .5f;
    

	// Use this for initialization
	void Start () {
        StartCoroutine(camControl());
	}
	
	// Update is called once per frame
	void Update () {
        }

    IEnumerator camControl()
    {
        mouseStart = Input.mousePosition;
        while (true)
        {
            if (!Input.GetMouseButton(0))
            {
                mouseOffset = Input.mousePosition - mouseStart;
                rotateTemp.y = mouseOffset.x * turnSpeed;
                rotateTemp.y = Mathf.Clamp(rotateTemp.y, maxLeft, maxRight);


                if (camRotationTemp >= maxLeft && rotateTemp.y < 0)
                {
                    camRotationTemp += rotateTemp.y;
                    transform.eulerAngles += rotateTemp;
                }
                if (camRotationTemp <= maxRight && rotateTemp.y > 0)
                {
                    camRotationTemp += rotateTemp.y;
                    transform.eulerAngles += rotateTemp;
                }
            }

            mouseStart = Input.mousePosition;
            
            yield return new WaitForSeconds(1/60f);

            //rotateTemp = transform.eulerAngles;
            //float mouseX = Input.GetAxis("Mouse X") * turnSpeed;
            //rotateTemp.y = Mathf.Clamp(rotateTemp.y + mouseX, -30f, 30f);
            //transform.eulerAngles = rotateTemp;
            //yield return new WaitForSeconds(1 / 60f);
        }
    }
}
