Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
67
Assets/Scripts/Player/CameraFx.cs
Normal file
67
Assets/Scripts/Player/CameraFx.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace GrassCutter
|
||||
{
|
||||
/// <summary>Trauma-based screen shake + FOV kicks, applied on top of whatever positions the camera.</summary>
|
||||
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<Camera>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>Extra sustained FOV (e.g. sprinting) that the camera eases toward.</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user