ViewPunch

From Valve Developer Community
Jump to: navigation, search

ViewPunch is part of CBasePlayer, which allows you to punch the player's views by a specified angle. It can be used in map logic with env_viewpunch.

Note.pngNote:This is only available on objects that derive from CBasePlayer!

Usage

//-----------------------------------------------------------------------------
// Purpose: Punch the player's view by the specified QAngle
// Input  : angleOffset- Angle to punch at
//-----------------------------------------------------------------------------
void CBasePlayer::ViewPunch( const QAngle &angleOffset )
{
	//See if we're suppressing the view punching
	if ( sv_suppress_viewpunch.GetBool() )
		return;

	// We don't allow view kicks in the vehicle
	if ( IsInAVehicle() )
		return;

	m_Local.m_vecPunchAngleVel += angleOffset * 20;
}

Examples

//This example gets the player, then punches their view and shoves them backwards.
//Then it shows you how to drop an object (pTarget) from the players hands. 
CBasePlayer *pPlayer = ToBasePlayer( pTarget );

if ( pPlayer != NULL )
{
	//Kick the player angles
	pPlayer->ViewPunch( QAngle( 20, 20, -30 ) );	

	Vector	dir = pPlayer->WorldSpaceCenter() - WorldSpaceCenter();
	VectorNormalize( dir );
	dir.z = 0.0f;
	
	Vector vecNewVelocity = dir * 250.0f;
	vecNewVelocity[2] += 128.0f;
	pPlayer->SetAbsVelocity( vecNewVelocity );

	color32 red = {128,0,0,128};
	UTIL_ScreenFade( pPlayer, red, 1.0f, 0.1f, FFADE_IN );
}

// Player takes less damage
float flDamage = ( pPlayer == NULL ) ? 250 : sk_hunter_dmg_charge.GetFloat();

// If it's being held by the player, break that bond
Pickup_ForcePlayerToDropThisObject( pTarget );

// Calculate the physics force
Hunter_ApplyChargeDamage( this, pTarget, flDamage );
//This example takes the player, punches his view
//pushes him back
CBasePlayer *pPlayer = ToBasePlayer( pHurt );

if ( pPlayer != NULL )
{
	//Kick the player angles
	if ( !(pPlayer->GetFlags() & FL_GODMODE ) && pPlayer->GetMoveType() != MOVETYPE_NOCLIP )
	{
		pPlayer->ViewPunch( viewPunch );

		Vector	dir = pHurt->GetAbsOrigin() - GetAbsOrigin();
		VectorNormalize(dir);

		QAngle angles;
		VectorAngles( dir, angles );
		Vector forward, right;
		AngleVectors( angles, &forward, &right, NULL );

		//Push the target back
		pHurt->ApplyAbsVelocityImpulse( - right * shove[1] - forward * shove[0] );
	}
}