CameraJuice.cs

A component that adds camera 'juice' effects. It applies a per-frame randomized shake offset to a target CameraComponent based on an intensity that decays over time, and provides methods to start a shake and to trigger a short global time scale 'hitstop'.

Native Interop
using Sandbox;
using System;
using System.Threading.Tasks;

public sealed class CameraJuice : Component
{
	public static CameraJuice Instance { get; private set; }

	[Property] public CameraComponent TargetCamera { get; set; }

	private float _shakeIntensity = 0f;
	private float _shakeDecay = 5f;

	protected override void OnStart()
	{
		Instance = this;
		if ( TargetCamera == null )
		{
			TargetCamera = Components.Get<CameraComponent>();
		}
	}

	protected override void OnUpdate()
	{
		if ( _shakeIntensity > 0f && TargetCamera.IsValid() )
		{
			Vector3 randomOffset = new Vector3(
				Game.Random.Float( -1f, 1f ),
				Game.Random.Float( -1f, 1f ),
				Game.Random.Float( -1f, 1f )
			) * _shakeIntensity;

			TargetCamera.LocalPosition += randomOffset * Time.Delta;
			_shakeIntensity = MathF.Max( 0f, _shakeIntensity - Time.Delta * _shakeDecay );
		}
	}

	public void Shake( float intensity = 15f, float decay = 8f )
	{
		_shakeIntensity = intensity;
		_shakeDecay = decay;
	}

	public async void TriggerHitstop( float durationSeconds = 0.05f )
	{
		Scene.TimeScale = 0.05f;
		await Task.DelaySeconds( durationSeconds );
		if ( Scene.IsValid() )
		{
			Scene.TimeScale = 1.0f;
		}
	}
}