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

public class CameraController : MonoBehaviour {

	public GameObject player;

	public float speed = 50f;

	private float movementThreshold = 1f;
	private float xDistanceDeadZone;
	private float zDistanceDeadZone;
	private Rect rect;
	private Vector3 tempPlayerPos;
	private Vector3 offset;
	
	// Use this for initialization
	void Start () {
		offset = transform.position - player.transform.position;
	}

	// Update is called once per frame
	void Update() {
		if (player.transform.position.x > transform.position.x)
		{
			xDistanceDeadZone = player.transform.position.x - transform.position.x;
		}
		else
		{
			xDistanceDeadZone = transform.position.x - player.transform.position.x;
		}

		if (player.transform.position.z > transform.position.z)
		{
			zDistanceDeadZone = player.transform.position.z - transform.position.z;
		}
		else
		{
			zDistanceDeadZone = transform.position.z - player.transform.position.z;
		}

		if (xDistanceDeadZone >= movementThreshold || zDistanceDeadZone >= movementThreshold)
		{
			float step = speed * Time.deltaTime;
			Vector3 offsetAndPosition = offset + player.transform.position;

			if (Vector3.Distance(player.transform.position, transform.position) < 1)
			{
				transform.position = offsetAndPosition;

			}else
			{
				transform.position = Vector3.Lerp(transform.position, offsetAndPosition, step);
			}
		}
	}
}
