Camera Bob
This tutorial will go through the basics of creating a camera bobbing for when a player walks. The result is a pretty realistic simulation of a head bob that nearly matches HL2's default walking speed and walking sounds. This tutorial has only been tested with the HL2MP OB and the template OB SDK, but should work for most every other version of Valve's source code with a few adjustmets. A special thanks goes out to sharkkk from the Steam Forums for figuring all of this stuff out.
The Code
In gamemovement.cpp, before the list of defines around line 65 or so, add the following:
// Camera Bob ConVar hf_bob_enabled ( "hf_bob_enabled", "1", 0, "head bobbing state", true, 0, true, 1 ); ConVar hf_bob_timer ( "hf_bob_timer", "0.4", 0, "time between bobs" ); ConVar hf_bob_scale ( "hf_bob_scale", "0.01", 0, "head bobbing scale");
bool bBobStep; float fBobTimer;
All we're doing here is declaring our convars, bools and floats for our camera bobbing code below.
In the same file, find the function void CGameMovement::WalkMove( void ) and add the following to the very top of the function:
if ( hf_bob_enabled.GetInt() == 1 ) { if ( player->GetAbsVelocity().Length() > 0 ){ if ( gpGlobals->curtime > fBobTimer ) { fBobTimer = gpGlobals->curtime + ( hf_bob_timer.GetFloat() / ( player->GetAbsVelocity().Length() / 200 ) ); if ( bBobStep == true ) { player->ViewPunch( QAngle( 1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat(), -1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat(), -1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat()) ); bBobStep = false; } else { player->ViewPunch( QAngle( 1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat(), 1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat(), -1 * player->GetAbsVelocity().Length() * hf_bob_scale.GetFloat()) ); bBobStep = true; } } } }
Basically, what's happening here is firing a bunch of view punches at certain intervals to simulate the bobbing. It's nothing too complicated, but it works quite well, and is very easy to manipulate according to what one has in mind.