using UnityEngine; namespace GrassCutter { /// Trauma-based screen shake + FOV kicks, applied on top of whatever positions the camera. public class CameraFx : MonoBehaviour { static CameraFx _i; public static Camera Cam { get; private set; } public static float BaseFov = 78f; float _trauma; float _fovKick, _fovTarget; Vector3 _offset; Quaternion _rotOffset = Quaternion.identity; public static float ShakeScale = 1f; public static float Trauma => _i ? _i._trauma : 0f; void Awake() { _i = this; Cam = GetComponent(); } /// /// Adds trauma, but each hit can only raise it to a ceiling set by its own size: a stream of tiny pops /// (finger, rolling pin, road roller) settles into a light buzz instead of stacking up into an earthquake. /// public static void Shake(float amount) { if (!_i || amount <= 0f || ShakeScale <= 0f) return; float ceiling = Mathf.Lerp(0.28f, 0.8f, Mathf.InverseLerp(0.05f, 0.6f, amount)) * Mathf.Min(1f, ShakeScale); if (_i._trauma < ceiling) _i._trauma = Mathf.Min(ceiling, _i._trauma + amount * ShakeScale); } public static void FovKick(float degrees) { if (_i) _i._fovKick = Mathf.Clamp(_i._fovKick + degrees * Mathf.Min(1f, ShakeScale), -6f, 6f); } /// Extra sustained FOV (e.g. sprinting) that the camera eases toward. public static void FovTarget(float extra) { if (_i) _i._fovTarget = extra; } void LateUpdate() { // undo last frame's offsets (the rig re-positions us each frame anyway, but stay safe) transform.localPosition -= _offset; transform.localRotation *= Quaternion.Inverse(_rotOffset); _trauma = Mathf.MoveTowards(_trauma, 0f, Time.deltaTime * 1.8f); float s = _trauma * _trauma; float t = Time.time * 24f; _offset = new Vector3(Mathf.PerlinNoise(t, 0.3f) - 0.5f, Mathf.PerlinNoise(0.7f, t) - 0.5f, 0f) * (0.07f * s); _rotOffset = Quaternion.Euler((Mathf.PerlinNoise(t, 5f) - 0.5f) * 3.5f * s, (Mathf.PerlinNoise(9f, t) - 0.5f) * 3.5f * s, (Mathf.PerlinNoise(t, 13f) - 0.5f) * 4.5f * s); transform.localPosition += _offset; transform.localRotation *= _rotOffset; _fovKick = Mathf.Lerp(_fovKick, 0f, Time.deltaTime * 9f); float target = BaseFov + _fovTarget + _fovKick; if (Cam) Cam.fieldOfView = Mathf.Lerp(Cam.fieldOfView, target, Time.deltaTime * 12f); } } }