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

public class ScreenShaker : MonoBehaviour {

    CinemachineVirtualCamera cam;
    CinemachineBasicMultiChannelPerlin noise;

    void Awake() {
        cam = GetComponent<CinemachineVirtualCamera>();
        noise = cam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
    }

    public void Shake(float magnitude = 1f, float duration = 0.2f) {
        StartCoroutine(ShakeCoroutine(magnitude, duration));
    }

    IEnumerator ShakeCoroutine(float magnitude, float duration) {
        noise.m_AmplitudeGain = magnitude;
        noise.m_FrequencyGain = 10f;

        yield return new WaitForSeconds(duration);

        noise.m_AmplitudeGain = 0f;
        noise.m_FrequencyGain = 1f;
    }

}
