using UnityEngine;
using System.Collections;

public class CameraControlScript : MonoBehaviour {

	public GameObject jogador;
	private Vector3 posAnteriorJogador;
	public float hDistance;
	public float vDistance;
	// Use this for initialization

	void Start () {
		jogador = GameObject.FindGameObjectWithTag ("Player");
		Vector3 posicaoCamera = jogador.transform.position;
		posicaoCamera.z = this.transform.position.z;
		this.gameObject.transform.position = posicaoCamera;
		this.posAnteriorJogador = this.jogador.transform.position;
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	void FixedUpdate() {
		Vector3 posJogador = this.jogador.transform.position;
		Vector3 posCamera = this.transform.position;
		Vector3 dist = new Vector3();
		if (Mathf.Abs (posJogador.x - posCamera.x) > hDistance  && jogador.rigidbody2D.velocity.magnitude > 0) {
			dist.x = -(this.posAnteriorJogador.x - posJogador.x);
		}

		if (Mathf.Abs (posJogador.y - posCamera.y) > vDistance) {
			dist.y = -(this.posAnteriorJogador.y - posJogador.y);
		}
		dist += this.transform.position;
		this.transform.position = dist;
		this.posAnteriorJogador = posJogador;
	}

}
