Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/sites/a/ on line 20
Wavelength | Your Game Editing Resource | Better Third Person Camera
Welcome, Guest! Login | Register

Better Third Person Camera [Print this Article]
Posted by: omega
Date posted: May 08 2004
User Rating: 4.5 out of 5.0
Number of views: 11936
Number of comments: 15
Description: Simple snippet for a better third person camera with player fading.
Here's a quick snippet for a functional 3rd person camera replacement.
(i've placed it in client-side coding because it's larger than a typical snippet, it's more or less a copy and paste tut :))

It's based on a version of my third person camera from one of my other mods and it also fades out the player when the camera gets close. The only thing this snippet doesn't show you is how to lift up the camera above the players head; however, this isn't hard to add. (Just remember to check for ceilings if you add this feature onto it!!)

I'm just going to toss all of the code right here and add comments explaining different pieces of it. Use it however you wish.

Read the notes about the lines marked with * at the bottom of the article!

 CODE (c++) 
#define CAM_MIN_DIST 4.0
#define CAM_MAX_DIST 48.0
extern Vector v_angles;
void DLLEXPORT CAM_Think( void )
    if( !cam_thirdperson )

    float maxDist = cam_idealdist->value;
    float DistFromWall;
    pmtrace_t tr;
    vec3_t origin, camForward;

    if (maxDist > CAM_MAX_DIST)
        maxDist = CAM_MAX_DIST; //cap
    DistFromWall = maxDist;
    cam_ofs[ 0 ] = v_angles[0];
    cam_ofs[ 1 ] = v_angles[1];
    cam_ofs[ 2 ] = v_angles[2];

    // Test camera position
//get the local player pointer
    cl_entity_t *player = gEngfuncs.GetLocalPlayer();      

*   static int frames = 0;      //static frame counter
*   if (frames < 5) frames++;    //cap at 5
* if (player && (gEngfuncs.GetMaxClients() > 0) && (gViewPort && gEngfuncs.pEventAPI) && frames >= 5)
        origin = player->origin;
        AngleVectors( cam_ofs, camForward, NULL, NULL );        //get the forward vector
        gEngfuncs.pEventAPI->EV_SetTraceHull(2);         //use duck hull for traces
        gEngfuncs.pEventAPI->EV_SetSolidPlayers(player->index - 1);
        gEngfuncs.pEventAPI->EV_PlayerTrace( origin, origin - (camForward * (maxDist + 8)), PM_STUDIO_BOX, -1, &tr );    //trace to maxDist +8 (leway for the bounding boxes)
        if ( tr.fraction < 1.0 )         //if a wall or object was hit..
            DistFromWall = maxDist * tr.fraction;   //calculate the final distance
            if( DistFromWall < CAM_MIN_DIST )    //if it's too close
                DistFromWall = CAM_MIN_DIST;    //push it to min dist
            player->curstate.renderamt = 255 * tr.fraction;      //calculate the players render amount based on distance from camera
            player->curstate.rendermode = kRenderTransColor; //use transcolor for transparency
        cam_ofs[ 2 ] = DistFromWall;    //pass  the distance off for the camera; if you want to keep the roll (z axis) then make DistFromWall a global or something (like v_angles) and read it in view.cpp instead of cam_ofs[2])

The frame counter and this part of if the statement "&& (gEngfuncs.GetMaxClients() > 0) && (gViewPort && gEngfuncs.pEventAPI) && frames >= 5" are only required if you start the game in third person. ie: +thirdperson on the command line, or you set cam_thirdperson 1 inside IN_Init() (ie: if you want to create a totally third person mod.) Corvidae showed me this ages ago when we were both working on mods that began in third person, he had done the camera before me and figured that out. Without these checks, the mod will crash before you get ingame (player pointer will be valid before everything else)

Rate This Article
This article is currently rated: 4.5 out of 5.0 (2 Votes)

You have to register to rate this article.
User Comments Showing comments 1-15

Posted By: jim_the_coder on May 09 2004 at 20:36:27
Many thanks omega, I tried to do something similar to this a while ago and ended up with invisible players...nice snippet!

Posted By: oinksalot on Jun 04 2004 at 07:45:57
Where do I put this? Replace the current 3rd person code in in_camera.cpp?

Posted By: Pongles on Jun 04 2004 at 16:53:30
yep, in in_camera.cpp.Edited by Pongles on Jun 04 2004, 16:54:13

Posted By: InternetNightmare on Jun 24 2004 at 19:54:58
I get invisible players :D

Posted By: -+Arcadian+- on Jul 01 2004 at 17:38:13
I haven't tested it yet, but you also need to include these ..?

#include "pmtrace.h"
#include "event_api.h"
#include "pm_defs.h"

Posted By: Unknown on Jul 23 2004 at 00:53:44
could you guys go about telling where i would place this in in_camera.cpp??
im confused on that part

Posted By: babywax on Jul 31 2004 at 03:26:11
Look for:
void DLLEXPORT CAM_Think( void )

Posted By: TawnosTUT on Dec 17 2004 at 14:32:02
THnx Omega, one good tut again. But how about that piece of the headheight or head over camera positions?
Im a bit confused about that.

Posted By: yodamod on Jun 01 2006 at 20:50:43
Thank you so much. I would also like to know what tawnos is talking about also

Posted By: Unbreakable on Oct 06 2006 at 14:13:36
Hey Omega.. I get an error about the CamThink function:
error C2601: 'CAM_Think' : local function definitions are illegal

How do I fix this?

Posted By: omega on Oct 06 2006 at 14:29:57
find where your missing ending brace ( } ) needs to go, before CAM_Think.

Posted By: Unbreakable on Oct 07 2006 at 18:38:49
ah ok Thanks.
edit: argh, I get more errors.
Can you explain a bit more of where to place the missing }Edited by Unbreakable on Oct 07 2006, 18:46:16

Posted By: cossack13 on Oct 26 2006 at 08:11:00
my camera is slightly to far to the right of the player and you get a semi side veiw of the character, were in the sdk do i change this (its the z axis i think).

Edit: fixed the problemEdited by cossack13 on Oct 26 2006, 09:09:32

Posted By: Unknown on Feb 01 2007 at 23:25:11
I have the same problem as cossack13 (wrong camera focus, in the z axis), and I still have not fixed this problem. How do I fix this?Edited by Unknown on Feb 02 2007, 15:33:10

Posted By: Unknown on Feb 10 2007 at 10:31:27
Take a look at "cam_idealyaw". Initialize it to 0 where appropriate.

You must register to post a comment. If you have already registered, you must login.

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 05 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005

Latest Comments
knock knock
General | News
By: MIFUNE | Dec 31 2017
knock knock
General | News
By: omega | Dec 22 2016
knock knock
General | News
By: MIFUNE | Oct 10 2015
New HL HUD Message System
Half-Life | Coding | Shared Tutorials
By: chbrules | Dec 31 2011
knock knock
General | News
By: Whistler | Nov 05 2011
Particle Engine tutorial part 4
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 18 2010
Particle Engine tutorial part 2
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
Particle Engine tutorial part 3
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
Game Movement Series #2: Analog Jumping and Floating
Half-Life 2 | Coding | Shared Tutorials
By: mars3554 | Oct 26 2009
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Deadpool | Aug 02 2009

Site Info
297 Approved Articless
8 Pending Articles
3940 Registered Members
0 People Online (4 guests)
About - Credits - Contact Us

Wavelength version:
Valid XHTML 1.0! Valid CSS!