using System.Collections; using System.Collections.Generic; using Cinemachine; using UnityEngine; public static class ScreenShake { static CinemachineVirtualCamera vCam; static ScreenShakeUpdate shakeUpdate; class ScreenShakeUpdate : MonoBehaviour { [HideInInspector] public float shakeTimer; [HideInInspector] public float shakeTimerTotal; [HideInInspector] public float startingIntensity; void Update() { if (shakeTimer > 0) { shakeTimer -= Time.deltaTime; CinemachineBasicMultiChannelPerlin multiChannelPerlin = vCam.GetCinemachineComponent(); multiChannelPerlin.m_AmplitudeGain = Mathf.Lerp(startingIntensity, 0f, 1 - (shakeTimer / shakeTimerTotal)); } } } /// Shake the camera /// It needs a cinemachine camera with a noise profile in it. /// public static void Shake(float intensity, float time) { if (vCam == null) { vCam = Camera.main.GetComponent().ActiveVirtualCamera.VirtualCameraGameObject .GetComponent(); } if (shakeUpdate == null) { shakeUpdate = new GameObject("ShakeUpdate").AddComponent(); } shakeUpdate.startingIntensity = intensity; shakeUpdate.shakeTimer = shakeUpdate.shakeTimerTotal = time; } }