using UnityEngine;
using System.Collections;

public class CameraControl : MonoBehaviour {
	Vector3 cameraStart;
	public float dirspeed4 = 1;
	GameObject player = GameObject.Find("tempChar");
	bool changePos;
	bool change = true;

	// Use this for initialization
	void Start () {
		cameraStart = new Vector3 (31.639f, -1.467f, -2.544f);
	}
	
	// Update is called once per frame
	void Update () {
		
		float step = 4 * Time.deltaTime;
		player = GameObject.Find ("tempChar");
		if (changePos) {
			cameraStart = player.transform.position;
			cameraStart.y += 7;
			changePos = false;
		}
		if (change) {
			Vector3 targetdir2 = cameraStart - transform.position;
			Vector3 newdir2 = Vector3.RotateTowards (transform.forward, targetdir2, step, 0.0f);
			transform.rotation = Quaternion.LookRotation (newdir2);
		}
		if (Input.GetKey("a")) {
			transform.Rotate(Vector3.down * dirspeed4);
			change = false;

		}
		if (Input.GetKey("d")) {
			transform.Rotate(Vector3.up * dirspeed4);
			change = false;

		}
		if (Input.GetKey("s")) {
			transform.Rotate (Vector3.right * dirspeed4);
			change = false;

		}
		if (Input.GetKey("w")) {
			transform.Rotate (Vector3.left * dirspeed4);
			change = false;

		}
		if(Input.GetKey(KeyCode.UpArrow)){
			changePos = true;
			Vector3 targetdir2 = cameraStart - transform.position;
			Vector3 newdir2 = Vector3.RotateTowards (transform.forward, targetdir2, step, 0.0f);
			transform.rotation = Quaternion.LookRotation (newdir2);
			change = true;
		}
		if(Input.GetKey(KeyCode.RightArrow)){
			changePos = true;
			Vector3 targetdir2 = cameraStart - transform.position;
			Vector3 newdir2 = Vector3.RotateTowards (transform.forward, targetdir2, step, 0.0f);
			transform.rotation = Quaternion.LookRotation (newdir2);
			change = true;
		}
		if(Input.GetKey(KeyCode.LeftArrow)){
			changePos = true;
			Vector3 targetdir2 = cameraStart - transform.position;
			Vector3 newdir2 = Vector3.RotateTowards (transform.forward, targetdir2, step, 0.0f);
			transform.rotation = Quaternion.LookRotation (newdir2);
			change = true;
		}
		if(Input.GetKey(KeyCode.DownArrow)){
			changePos = true;
			Vector3 targetdir2 = cameraStart - transform.position;
			Vector3 newdir2 = Vector3.RotateTowards (transform.forward, targetdir2, step, 0.0f);
			transform.rotation = Quaternion.LookRotation (newdir2);
			change = true;
		}
		Quaternion q = transform.rotation;
		q.eulerAngles = new Vector3 (q.eulerAngles.x, q.eulerAngles.y, 0);
		transform.rotation = q;
	}
}
