TTT thirdperson code help


Adding collision code isn’t as difficult as it may first seem. Basically you just need to do a trace from your original, unmodified, origin to the thirdperson calculated origin. If the trace hits something, modify the origin.

You can use a trace-hull to prevent clipping by specifying if the cube collides then you’ll be just a bit outside of the wall; it can be done by just modifying the normal a few units more. Either will work just fine.

You can simplify even further, if you don’t want to check for a hit; you can always just set the view.origin to the trace.HitPos because it’ll either be the wall, or the end point on the trace.

So, it’d just be an addition to your code that would look like:

hook.Add( "CalcView", "ThirdpersonFreecam:CalcView", function( _p, _origin, _ang, _fov, _znear, _zfar )
	local view = { };
	view.origin 		= _origin;


	if ( _bThirdPerson ) then
		local _targetOrigin = ...;
		local _wallOffset = 4;
		local _trdata = {
			start	= _origin;
			endpos	= _targetOrigin;
			filter	= player.GetAll( );
			mins	= Vector( -_wallOffset, -_wallOffset, -_wallOffset );
			maxs	= Vector( _wallOffset, _wallOffset, _wallOffset );
		local _tr = util.TraceHull( _trdata );

		view.origin 	= _tr.HitPos;


		return view;