Console command and input system for toggling a noclip movement mode. Defines a ConCmd "noclip" that finds the local PlayerController, creates or gets a NoclipMoveMode component and toggles its Enabled flag, and a GameObjectSystem that listens for the V key to call the toggle when the mouse cursor is hidden.
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();
}
}