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

public class ScreenShake : MonoBehaviour 
{
	[SerializeField]
	private Vector3 originPos;

	public void StartShake (float duration)
	{
		StartCoroutine (Shake (duration));
	}

	IEnumerator Shake (float duration)
	{
		float elapsed = 0.0f;

		originPos = this.transform.position;

		while (elapsed < duration)
		{
			elapsed += Time.deltaTime;

			float x = Random.value * 2.0f - 1.0f;
			float y = Random.value * 2.0f - 1.0f;

			this.transform.position = new Vector3 (x, y, originPos.z);

			yield return null;
		}
		this.transform.position = originPos;
	}

}
