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

public class ShakeCamera : MonoBehaviour
{
     float shakeDuration = 1f;
     float shakeMagnitude = 0.2f;

    Vector3 initialPosition;
    void Start()
    {
        initialPosition = transform.position;
    }

    public void PlayCameraShake(float shakeTime)
    {
        shakeDuration = shakeTime;
        StartCoroutine(ShakeCameraAction());
    }

    IEnumerator ShakeCameraAction()
    {
        float elapsedTime = 0;
        while (elapsedTime < shakeDuration)
        {
            transform.position = initialPosition + (Vector3)Random.insideUnitCircle * shakeMagnitude;
            elapsedTime += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }

        transform.position = initialPosition;
    }
}
