using UnityEngine;
using System.Collections;

public class CarCamera : MonoBehaviour {

	public Transform car;
	public float distance = 6.4f;
	public float height = 2.0f;
	private Vector3 rotationVector;
	private bool paused = false;

	void  Start (){
	}

	void Update() {
		Pause();
	}

	void Pause() {
		if (Input.GetKey(KeyCode.P)){
			if(paused) {
				Time.timeScale = 1;
				paused = false;
			} else {
				Time.timeScale = 0;
				paused = true;
			}
		}
	}

	
	void  LateUpdate (){
		float wantedAngel= car.eulerAngles.y;
		float wantedHeight= car.position.y + height;
		float myAngel= transform.eulerAngles.y;
		Vector3 myTempHeight = transform.position;
		float myHeight = myTempHeight.y;

		myAngel = Mathf.LerpAngle(myAngel,wantedAngel,3.0f*Time.deltaTime);
		myHeight = Mathf.Lerp(myHeight,wantedHeight,2.0f*Time.deltaTime);
		Quaternion currentRotation= Quaternion.Euler(0,myAngel,0);
		transform.position = car.position;
		transform.position -= currentRotation*Vector3.forward*distance;

		Vector3 tempPos = transform.position;
		tempPos.y = myHeight;
		transform.position = tempPos;
		transform.LookAt(car);
	}
}