CameraWobble.cs
using System;
using System.Linq;
using Sandbox;
public sealed class CameraWobble : Component
{
[Property] public float Intensity { get; set; } = 0.0f;
[Property] public float Speed { get; set; } = 1.2f;
private Rotation _originalLocalRotation;
private CameraComponent _cameraComponent;
private float _baseFieldOfView = 80f;
protected override void OnStart()
{
_originalLocalRotation = LocalRotation;
// Find the camera component on this object or its children
_cameraComponent = Components.GetInDescendantsOrSelf<CameraComponent>();
if ( _cameraComponent != null )
{
_baseFieldOfView = _cameraComponent.FieldOfView;
}
}
protected override void OnUpdate()
{
if ( Intensity <= 0.0f )
{
LocalRotation = _originalLocalRotation;
if ( _cameraComponent != null )
_cameraComponent.FieldOfView = _baseFieldOfView;
return;
}
// Layered sine waves for the rolling warble
float t = Time.Now * Speed;
float pitch = (MathF.Sin( t * 0.8f ) + (MathF.Sin( t * 2.2f ) * 0.3f)) * Intensity * 3.0f;
float yaw = (MathF.Cos( t * 0.6f ) + (MathF.Cos( t * 1.7f ) * 0.3f)) * Intensity * 3.0f;
float roll = MathF.Sin( t * 0.4f ) * Intensity * 4.0f;
var wobbleRotation = Rotation.From( pitch, yaw, roll );
LocalRotation = _originalLocalRotation * wobbleRotation;
// Focus push-pull effect (breathing FOV warp)
if ( _cameraComponent != null )
{
float fovOffset = MathF.Sin( t * 0.5f ) * Intensity * 2.5f;
_cameraComponent.FieldOfView = _baseFieldOfView + fovOffset;
}
}
}