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

public class CameraShaker : MonoBehaviour
{
    public Transform CameraTransform;

    public float ShakeIntensity = 1;
    public float MaxShakeDistance = 0.05f;
    public float MinShakeWaitTime = 0.05f;
    public float MaxShakeWaitTime = 0.1f;
    public float LerpSpeed = 1;

    private Vector2 ShakePosition = Vector2.zero;

    private void Start()
    {
        StartCoroutine(ShakeRoutine());
    }

    private IEnumerator ShakeRoutine()
    {
        while(true)
        {
            Vector2 Dir = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)).normalized;
            ShakePosition = Dir * Random.Range(0, MaxShakeDistance) * ShakeIntensity;
            yield return new WaitForSeconds(Random.Range(MinShakeWaitTime, MaxShakeWaitTime));
        }
    }

    // Update is called once per frame
    void Update()
    {
        CameraTransform.localPosition = Vector3.Lerp(CameraTransform.localPosition, ShakePosition, Time.smoothDeltaTime * LerpSpeed);
    }
}
