Making The Player Fly

From Valve Developer Community
Jump to: navigation, search
Wikipedia - Letter.png
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
Dead End - Icon.png
This article has no Wikipedia icon links to other VDC articles. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

It is possible to make the player fly around the level like a bird or a drone. The player will still collide with the world and it will stay at it's normal walking or sprinting speed. This code has been tested on Source SDK 2013 SP, but it should work on MP or even 2007.

We will start by adding the player FLY flag if it already isn't there. Open const.h on the Client Project and check for a FL_FLY flag which is around line 159. If there isn't one, add:

#define	FL_FLY					(1<<11)

above FL_SWIM. Now head into the server project and open client.cpp. We will be adding in our flying function there. You can copy and paste the godmode function and command to use it as a base. I'll be doing that here.

void CC_God_f (void)
{
	if ( !sv_cheats->GetBool() )
		return;

	CBasePlayer *pPlayer = ToBasePlayer( UTIL_GetCommandClient() ); 
	if ( !pPlayer )
		return;

#ifdef TF_DLL
   if ( TFGameRules() && ( TFGameRules()->IsPVEModeActive() == false ) )
   {
	   if ( gpGlobals->deathmatch )
		   return;
   }
#else
	if ( gpGlobals->deathmatch )
		return;
#endif

	pPlayer->ToggleFlag( FL_GODMODE );
	if (!(pPlayer->GetFlags() & FL_GODMODE ) )
		ClientPrint( pPlayer, HUD_PRINTCONSOLE, "godmode OFF\n");
	else
		ClientPrint( pPlayer, HUD_PRINTCONSOLE, "godmode ON\n");
}

static ConCommand god("god", CC_God_f, "Toggle. Player becomes invulnerable.", FCVAR_CHEAT );

First off, change the function name to CC_Fly_f and rename the ConCommand to fly. Unless this can be toggled on and off by the player, keep the FCVAR_CHEAT flag. While you're changing the name of the function, you can remove the TF2 code. We won't be needing that here.

void CC_Fly_f (void)
{
	if ( !sv_cheats->GetBool() )
		return;

	CBasePlayer *pPlayer = ToBasePlayer( UTIL_GetCommandClient() ); 
	if ( !pPlayer )
		return;

	pPlayer->ToggleFlag( FL_GODMODE );
	if (!(pPlayer->GetFlags() & FL_GODMODE ) )
		ClientPrint( pPlayer, HUD_PRINTCONSOLE, "godmode OFF\n");
	else
		ClientPrint( pPlayer, HUD_PRINTCONSOLE, "godmode ON\n");
}

static ConCommand fly("fly", CC_Fly_f, "Toggle. Player is able to fly.", FCVAR_CHEAT );

We can change around the FL_GODMODE flags to FL_FLY flags.

pPlayer->ToggleFlag( FL_FLY );
	if (!(pPlayer->GetFlags() & FL_FLY ) )
		ClientPrint( pPlayer, HUD_PRINTCONSOLE, "fly OFF\n");
	else
		ClientPrint( pPlayer, HUD_PRINTCONSOLE, "fly ON\n");

Finally, in the middle of checking for cheats and the messages, you can add this block of code which can make the player fly depending on the flag state.

if (pPlayer->GetFlags() & FL_FLY)
	{
		pPlayer->SetMoveType(MOVETYPE_FLY, MOVECOLLIDE_FLY_BOUNCE);
		//pPlayer->SetDefaultFOV(130); I personally put this here, but it's not required.
 	}
	else {
		pPlayer->SetMoveType(MOVETYPE_WALK, MOVECOLLIDE_DEFAULT);
                //pPlayer->SetDefaultFOV(90); I personally put this here, but it's not required.
	}

Your finished function should look something like this.

void CC_Fly_f()
{
	CBasePlayer * pPlayer = UTIL_GetLocalPlayer();

	if (!sv_cheats->GetBool())
		return;

	if (pPlayer->GetFlags() & FL_FLY)
	{
		pPlayer->SetMoveType(MOVETYPE_FLY, MOVECOLLIDE_FLY_BOUNCE);
                //pPlayer->SetDefaultFOV(130); I personally put this here, but it's not required.
	}
	else {
		pPlayer->SetMoveType(MOVETYPE_WALK, MOVECOLLIDE_DEFAULT);
                //pPlayer->SetDefaultFOV(90); I personally put this here, but it's not required.
	}

	pPlayer->ToggleFlag(FL_FLY);
	if (!(pPlayer->GetFlags() & FL_FLY))
		ClientPrint(pPlayer, HUD_PRINTCONSOLE, "flying OFF\n");
	else
		ClientPrint(pPlayer, HUD_PRINTCONSOLE, "flying ON\n");

}

static ConCommand fly("fly", CC_Fly_f, "Toggle. Player is able to fly.", FCVAR_CHEAT );

Now compile the solution and load up your mod. Make sure you have sv_cheats on and type in "fly". To control the player, look up and press W. You can also control the player by using S and the strafe keys.