NoclipCommands.cs

Console command and input system for toggling a noclip movement mode. Defines a ConCmd "noclip" that finds the local PlayerController, adds or gets a NoclipMoveMode component and toggles its Enabled state. Also a GameObjectSystem listens for the V key (and ignores input when the mouse cursor is visible) and calls the same toggle.

Networking
public static class NoclipCommands
{
	[ConCmd( "noclip", Help = "Toggle noclip fly mode" )]
	public static void ToggleNoclip()
	{
		var controller = Game.ActiveScene?.GetAllComponents<PlayerController>()
			.FirstOrDefault( x => x.IsValid() && !x.IsProxy );

		if ( controller is null )
		{
			Log.Warning( "noclip: no local PlayerController found in the active scene." );
			return;
		}

		var mode = controller.Components.Get<NoclipMoveMode>( true )
			?? controller.Components.Create<NoclipMoveMode>( false );

		mode.Enabled = !mode.Enabled;
		Log.Info( mode.Enabled ? "Noclip enabled" : "Noclip disabled" );
	}
}

public sealed class NoclipInputSystem : GameObjectSystem
{
	public NoclipInputSystem( Scene scene ) : base( scene )
	{
		Listen( Stage.StartUpdate, 0, OnUpdate, "Noclip.VKey" );
	}

	void OnUpdate()
	{
		if ( Input.MouseCursorVisible )
			return;

		if ( Input.Keyboard.Pressed( "v" ) )
			NoclipCommands.ToggleNoclip();
	}
}