Adding a Camera Tilt When Moving Sideways

From Valve Developer Community
Jump to navigation Jump to search

This is a simple tutorial for creating a camera tilt when player moves sideways. This tutorial was inspired by similar feature from Black Mesa Black Mesa.

Note.pngNote: The Source Engine actually has a built-in alternative, which is the sv_rollangle command, but the implementation described here is more configurable and is not a launch-only command.


The Code

Open src/game/shared/gamemovement.cpp and before all the defines, add these ConVars around line 60:

ConVar cl_viewtilt_enabled( "cl_viewtilt_enabled", "1", 0, "Side movement camera tilt" );
ConVar cl_viewtilt_scale( "cl_viewtilt_scale", "0.1", 0, "Tilt strength" );

In this part of the code, we are declaring ConVars that will help customize this feature from the game's console.

Then we go to void CGameMovement::WalkMove( void ) and add this code after around line 1920:

if ( cl_viewtilt_enabled.GetInt() && !engine->IsPaused() )
{
    float speed = max( fabs(mv->m_flForwardMove) + fabs(mv->m_flSideMove), 1.0f );
    float side = mv->m_flSideMove / speed;
    float tilt = side * cl_viewtilt_scale.GetFloat();
    player->ViewPunch( QAngle(0, 0, tilt) );
}

In this part of the code, we are implementing a view tilt effect that responds to the player's sideways movement.

Different camera tilt for walking and sprinting

If you want the camera tilt to vary depending on whether the player is sprinting, after these lines:

ConVar cl_viewtilt_enabled( "cl_viewtilt_enabled", "1", 0, "Side movement camera tilt" );
ConVar cl_viewtilt_scale( "cl_viewtilt_scale", "0.1", 0, "Tilt strength" );

Add this:

ConVar cl_viewtilt_sprint_multiplier( "cl_viewtilt_sprint_multiplier", "2", 0, "Tilt strength multiplier for sprinting");

This new ConVar will help us scale how strong the camera tilt is supposed to be when sprinting, relative to the camera tilt when just walking.

and then change the code in void CGameMovement::WalkMove( void ) from this:

if ( cl_viewtilt_enabled.GetInt() && !engine->IsPaused() )
{
    float speed = max( fabs(mv->m_flForwardMove) + fabs(mv->m_flSideMove), 1.0f );
    float side = mv->m_flSideMove / speed;
    float tilt = side * cl_viewtilt_scale.GetFloat();
    player->ViewPunch( QAngle(0, 0, tilt) );
}

To this:

if ( cl_viewtilt_enabled.GetInt() && !engine->IsPaused() )
{
    float speed = max( fabs(mv->m_flForwardMove) + fabs(mv->m_flSideMove), 1.0f );
    float side = mv->m_flSideMove / speed;
    float tilt = side * cl_viewtilt_scale.GetFloat();
    if( player->m_nButtons & IN_SPEED )
        tilt *= cl_viewtilt_sprint_multiplier.GetFloat(); // Sprinting multiplier
    
    player->ViewPunch( QAngle(0, 0, tilt) );
}

In this part of the code, we are adding a new if statement that will check if the player is sprinting, and if so, multiply the default tilt value by the multiplier from the ConVar we added earlier.

Credits

Editing/Usage

You are free to use and edit this code in your projects as much as you want.