using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour {
	public GameObject follow;
	private Vector3 sPos;
	private Vector3 sC;
	private Vector3 oldSC = Vector3.zero;
	private float force = 0.02f;
	void Start () {
		
		Vector3 pos = transform.position;
		sC = new Vector3 (pos.x,  pos.y, pos.z);
		Vector3 p = follow.transform.position;
		sPos = new Vector3 (p.x, p.y, p.z);

		p = follow.transform.position;
		oldSC = new Vector3 (sPos.x - (sPos.x - p.x) - 1, 0, sPos.z - (sPos.z - p.z) - 1);


	}
		
	void Update () {
		Quake ();
	}

	void Quake()
	{
		float heightScale = WallsManager.AllWallsHeight;
		Vector3 pos = new Vector3 (Random.Range (-force, force), Random.Range (-force, force), Random.Range (-force, force));
		Vector3 p = follow.transform.position;
		sC = new Vector3 (sPos.x - (sPos.x - p.x) - 1, 0, sPos.z - (sPos.z - p.z) - 1);
		oldSC = new Vector3 (oldSC.x * .97f + sC.x * .03f, 0, oldSC.z * .97f + sC.z * .03f);

		if (heightScale < .4 && heightScale > -.25) 
		{
			transform.position = new Vector3 (oldSC.x + pos.x, 1, oldSC.z + pos.z);
		} else {
			transform.position = new Vector3 (oldSC.x,1,oldSC.z);
		}
	}



}
