FrontViewController.cs

Component that toggles a front-facing camera view for the player. It finds a PlayerController and the main CameraComponent, listens for an input named "FrontViewController" to toggle, and when active it positions and rotates the camera in front of the player and disables controller camera input.

using Sandbox;
using System.Linq;

public sealed class FrontViewController : Component
{
    [Property] public PlayerController PlayerController { get; set; }
    [Property] public CameraComponent Camera { get; set; }

    [Property] public float Distance { get; set; } = 130f;
    [Property] public float Height { get; set; } = 45f;

    private bool isFrontView = false;

    protected override void OnStart()
    {
        base.OnStart();
        PlayerController ??= Components.Get<PlayerController>();
        Camera ??= Scene.GetAllComponents<CameraComponent>().FirstOrDefault( x => x.IsMainCamera );
    }

    protected override void OnUpdate()
    {
        base.OnUpdate();

        if ( Input.Pressed( "FrontViewController" ) )
        {
            isFrontView = !isFrontView;
        }
    }

    protected override void OnPreRender()
    {
        base.OnPreRender();

        if ( Camera == null ) return;

        if ( isFrontView )
        {
            if ( PlayerController != null )
            {
                PlayerController.UseCameraControls = false;
            }

            Camera.LocalPosition = Vector3.Forward * Distance + Vector3.Up * Height;
            Camera.LocalRotation = Rotation.From( 5, 180, 0 );
        }
        else
        {
            if ( PlayerController != null && !PlayerController.UseCameraControls )
            {
                PlayerController.UseCameraControls = true;
            }
        }
    }
}