FollowCam.cs
using Sandbox;
public sealed class FollowCamera : Component
{
[Property] public GameObject Target { get; set; }
[Property] public Vector3 Offset { get; set; } = new Vector3( -200, 0, 100 );
[Property] public float Smoothness { get; set; } = 0.1f;
protected override void OnUpdate()
{
if ( Target == null ) return;
// Calculate the target position with offset
var targetPosition = Target.WorldPosition + Offset;
// Smoothly interpolate the camera position
WorldPosition = Vector3.Lerp( WorldPosition, targetPosition, Time.Delta / Smoothness );
// Make the camera look at the target
WorldRotation = Rotation.LookAt( Target.WorldPosition - WorldPosition );
}
}