CustomTopDownController.cs

A game Component for a top-down player controller. It reads input to set movement velocity and smoothly rotates the player toward movement direction, and positions the scene camera above and behind the player each frame.

Native Interop
using Sandbox;

public sealed class CustomTopDownController : Component
{
	[RequireComponent] public PlayerController Controller { get; set; }

	protected override void OnFixedUpdate()
	{
		if ( IsProxy ) return;
		if ( !Controller.IsValid() ) return;

		// Move player when pressing WASD / AnalogMove
		var speed = Input.Down( "Run" ) ? Controller.RunSpeed : Controller.WalkSpeed;
		Controller.WishVelocity = Input.AnalogMove * speed;

		// Rotate player to face movement direction
		if ( Controller.WishVelocity.LengthSquared > 0 )
		{
			var targetAngle = Controller.WishVelocity.EulerAngles;
			Controller.EyeAngles = Rotation.Slerp( Controller.EyeAngles, targetAngle, Time.Delta * 12f );
		}
	}

	protected override void OnPreRender()
	{
		if ( IsProxy ) return;
		if ( !Controller.IsValid() || !Scene.Camera.IsValid() ) return;

		// Top-down camera follow
		Scene.Camera.WorldPosition = Controller.WorldPosition + Vector3.Up * 1024f + Vector3.Backward * 256f;
		Scene.Camera.WorldRotation = new Angles( 75, 0, 0 );
	}
}