using UnityEngine;
using System.Collections;

public class CameraMove : MonoBehaviour {

    private Vector3 ResetCamera;
    private Vector3 Origin;
    private Vector3 Diference;
    private bool Drag = false;
    void Start()
    {
        ResetCamera = Camera.main.transform.position;
    }
    void LateUpdate()
    {
        if (Input.GetMouseButton(0))
        {
            //DrawRay( [위치 값], [어느 방향+어느 길이], [색상] ): 일정 영역에 디버그용 선(Ray)을 그림
            Debug.DrawRay(transform.position, this.transform.forward * 10.0f, Color.red);

            //충돌이 감지된 영역
            RaycastHit2D hit;

            //마우스 포이트 근처 좌표를 만든다.
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            //마우스 근처에 오브젝트가 있는지 확인
            hit = Physics2D.Raycast(ray.origin, ray.direction * 100);
            if (hit)
            {
                //있다!

                //있으면 오브젝트를 저장한다.
                GameObject target = hit.collider.gameObject;
                if (target.CompareTag("Player"))
                    return;
            }

            Diference = (Camera.main.ScreenToWorldPoint(Input.mousePosition)) - Camera.main.transform.position;

            if (Drag == false)
            {
                Drag = true;
                Origin = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            }
        }
        else
        {
            Drag = false;
        }

        if (Drag == true)
        {
            Camera.main.transform.position = Origin - Diference;
        }

        //RESET CAMERA TO STARTING POSITION WITH RIGHT CLICK
        if (Input.GetMouseButton(1))
        {
            Camera.main.transform.position = ResetCamera;
        }
    }
}
