Cleanup is going to take awhile, so the site is back up but editing has been disabled.

Third Person Camera

From HalfLife 2 Knowledge Base

Jump to: navigation, search

by <SB> Childe Roland

There are a few articles on Wavelegth that deal with making a better Third Person camera view. These are good links to check and perhaps implement in your mod, however, neither of them deals with the problem encountered while using the camera in a vehicle. (Those who have tried know what I'm talking about.) This page shows how to get the camera working properly with the vehicles. I am not very articulate, so, I'll let the code do the talking.


All the fixes here will assume that you have already implemented the code found in that first link. The second link is optional and just has a few fixes for the first one.


  • First, you will probably want to set the Max camera distance higher, so you can see the entire car
  • Next, this part comes from the first article above:
if( !C_BasePlayer::GetLocalPlayer() ) 
		{// this code can be hit from the main menu, where it will crash
			camAngles[ 2 ] = dist; // if there's no localplayer to calc from
		}
		else 
		{
			trace_t tr;
			C_BasePlayer* localPlayer = C_BasePlayer::GetLocalPlayer();
			Vector origin = localPlayer->GetLocalOrigin(); // find our player's origin
			origin += localPlayer->GetViewOffset(); // and from there, his eye position 
			AngleVectors( QAngle(camAngles.x, camAngles.y, camAngles.z), &camForward, NULL, NULL ); // get the forward vector 

Replace that with this:

 			C_BasePlayer* localPlayer = C_BasePlayer::GetLocalPlayer();
			Vector origin;
			if( localPlayer->IsInAVehicle () )
			{
				IClientVehicle *pVehicle = localPlayer->GetVehicle();
				int nRole = pVehicle->GetPassengerRole( localPlayer );
				pVehicle->GetVehicleViewPosition(nRole,&origin,&viewangles);

				AngleVectors( viewangles, &camForward, NULL, &camUp ); // get the forward/up vectors
				camAngles[ 0 ] = viewangles.x + cam_carpitch.GetFloat();
				camAngles[ 1 ] = viewangles.y;
				camAngles[ 2 ] = f_carDist;
				//DevMsg(1, "IsInAVehicle CamAngles  X:%d   |Y:%d|   Z:%d\n", camAngles[ 0 ] , camAngles[ 1 ], camAngles[ 2 ] );
			}
			else
			{
				origin = localPlayer->GetLocalOrigin(); // find our player's origin
				origin += localPlayer->GetViewOffset(); // and from there, his eye position 

				AngleVectors( QAngle(camAngles.x, camAngles.y, camAngles.z),
				&camForward, NULL, &camUp ); // get the forward/up vectors
			}
Personal tools