A spectator camera component for a sandboxed game. It reads mouse and keyboard input to rotate and move a CameraComponent, updating field of view and the component's WorldPosition and WorldRotation each frame.
using Sandbox;
public class Spectator : Component
{
CameraComponent c;
protected override void OnStart()
{c = Scene.Camera;}
float x;
float y;
float z;
protected override void OnUpdate()
{ if (IsProxy) return;
var Delta = Input.MouseDelta * Time.Delta * Preferences.Sensitivity;
c.FieldOfView = Preferences.FieldOfView;
x = (x + Delta.x) % 360;
y = (y - Delta.y).Clamp(-89f, 89f);
z = Input.Down("shift") ? 512 : 256;
c.WorldRotation = new Angles(-y, -x, 0);
c.WorldPosition = WorldPosition;
WorldPosition += c.WorldRotation * (Input.Down("w") ? new Vector3(z, 0, 0) * Time.Delta : Vector3.Zero);
WorldPosition += c.WorldRotation * (Input.Down("a") ? new Vector3(0, z, 0) * Time.Delta : Vector3.Zero);
WorldPosition += c.WorldRotation * (Input.Down("s") ? new Vector3(-z, 0, 0) * Time.Delta : Vector3.Zero);
WorldPosition += c.WorldRotation * (Input.Down("d") ? new Vector3(0, -z, 0) * Time.Delta : Vector3.Zero);
WorldPosition += c.WorldRotation * (Input.Down("space") ? new Vector3(0, 0, z) * Time.Delta : Vector3.Zero);
WorldPosition += c.WorldRotation * (Input.Keyboard.Down("c") ? new Vector3(0, 0, -z) * Time.Delta : Vector3.Zero);
}
}