components/spectator.cs

A spectator camera component for the game. It attaches to the scene camera GameObject on start, reads mouse/keyboard input each frame to rotate and move the camera, and applies sensitivity and field of view from Preferences.

Native Interop
using Sandbox;
using System;

public class Spectator : Component
{
    GameObject c;
    protected override void OnStart()
    {if (IsProxy) return;
    c = Scene.Camera.GameObject;}
    
    float x = 0;
    float y = 0;
    float z = 0;
    
    protected override void OnUpdate()
    { if (IsProxy || new Game.Overlay().IsPauseMenuOpen) return;
        var Delta = Input.MouseDelta * Time.Delta * Preferences.Sensitivity;
        x += Delta.x; y -= Delta.y;
        y = Math.Clamp(y, -89, 89);
        z = Input.Down("Run") ? Time.Delta * 800 : Time.Delta * 400;
        
        c.WorldRotation = new Angles(-y, -x, 0);
        c.WorldPosition += Input.Down("Forward") ? c.WorldRotation * new Vector3(z, 0, 0) : 0;
        c.WorldPosition += Input.Down("Backward") ? c.WorldRotation * new Vector3(-z, 0, 0) : 0;
        c.WorldPosition += Input.Down("Left") ? c.WorldRotation * new Vector3(0, z, 0) : 0;
        c.WorldPosition += Input.Down("Right") ? c.WorldRotation * new Vector3(0, -z, 0) : 0;
    c.GetComponent<CameraComponent>().FieldOfView = Preferences.FieldOfView;}
}