Thirdperson Camera Collision Problem

So, here is my code:

[lua]
if ( CLIENT ) then

local on = false – default off

local function toggle()

on = !on

if on == true then

	print( 'enabled' )
	
	LocalPlayer():PrintMessage( HUD_PRINTTALK, "Third person mode enabled." )

else

	print( 'disabled')
	
	LocalPlayer():PrintMessage( HUD_PRINTTALK, "Third person mode disabled." )

end

end

hook.Add( “ShouldDrawLocalPlayer”, “ThirdPersonDrawPlayer”, function()

if on and LocalPlayer():Alive() then

	return true

end

end )

hook.Add( “CalcView”, “ThirdPersonView”, function( ply, pos, angles, fov )

if on and ply:Alive() then

–[[
local view = {}
view.origin = pos - ( angles:Forward() * 70 ) + ( angles:Right() * 20 ) + ( angles:Up() * 5 )
–view.origin = pos - ( angles:Forward() * 70 )
view.angles = ply:EyeAngles() + Angle( 1, 1, 0 )
view.fov = fov

	return GAMEMODE:CalcView( ply, view.origin, view.angles, view.fov )

–]]

	local view = {};
	local dist = 100;
	local trace = {};
		
	trace.start = pos;
	trace.endpos = pos - ( angles:Forward() * dist );
	trace.filter = LocalPlayer();
	local trace = util.TraceLine( trace );
	if( trace.HitPos:Distance( pos ) < dist - 10 ) then
		dist = trace.HitPos:Distance( pos ) - 10;
	end;
	view.origin = pos - ( angles:Forward() * dist );
	view.angles = angles;
	view.fov = fov;
	
	return view;

end

end )

concommand.Add( “thirdperson_toggle”, toggle )

end

if ( SERVER ) then

function ulx.thirdperson( calling_ply )

calling_ply:SendLua([[RunConsoleCommand("thirdperson_toggle")]])	

end
local thirdperson = ulx.command( “Custom”, “ulx thirdperson”, ulx.thirdperson, {"!thirdperson", “!3p”}, true )
thirdperson:defaultAccess( ULib.ACCESS_ALL )
thirdperson:help( “Toggles third person mode” )

end – end serverside
[/lua]

Everything works fine but the following bug happens:

As you can see, the camera collide works fine… But it will clip through the walls… Obviously, this will be a problem in DarkRP.

Please help! Thanks :slight_smile:

Are you serious? How is it obvious that its a problem in DarkRP?
Please, explain the connection.

If the camera clips through walls you can see into peoples bases.

Check out

util.TraceHull

Ahh right, my apologizes to the OP, I somehow read it that he meant that the problem is caused by the DarkRP files.

its a problem because youre not using util.TraceHull

So where exactly would I put this into the script? Please excuse my incompetence in Lua, I’m new.

Replace your TraceLine with TraceHull, and add “mins” and “maxs” to the structure. Those are the dimensions of the hull, the space you want to leave between the wall and the camera.

My code is as follows now:

[lua]

hook.Add( “CalcView”, “ThirdPersonView”, function( ply, pos, angles, fov )

if on and ply:Alive() then

	local view = {};
	local dist = 100;
	local trace = {};
		
	trace.start = pos;
	trace.endpos = pos - ( angles:Forward() * dist );
	trace.filter = LocalPlayer();
	trace.mins = pos;
	trace.maxs = pos;

	local trace = util.TraceHull( trace );
	if( trace.HitPos:Distance( pos ) < dist - 10 ) then
		dist = trace.HitPos:Distance( pos ) - 10;
	end;
	view.origin = pos - ( angles:Forward() * dist );
	view.angles = angles;
	view.fov = fov;
	
	return view;

end

end )

[/lua]

It returned an error. I would tell you the error but I’m pretty sure you know what it is. I don’t know what to put after the mins and maxs… It’s so different from everything else I’ve been doing… Please help!

The wiki page for the TraceHull function is very detailed and has a bunch of examples, are you sure you’ve looked over all of them? They are pretty user friendly

Also check the TraceHull structure page to see what mins and maxs is. You can’t just put the position there.

So maxs/mins returns vectors… I see that but I still just don’t understand what I’d put in there?

For example:



maxs: The 3D vector local to the start/endpos with the highest values. This will represent the corner with the upper bounds of the box.


I don’t understand what this would represent… I have no clue what I’d put in there…

They don’t return anything, they are variables. Both are local vectors. The hull is a box, mins is one of the bottom corners, and maxs is the top opposite corner.

[editline]10th March 2016[/editline]

Try this (I haven’t tested it):


trace.mins = Vector(-20,-20,-20);
trace.maxs = Vector(20,20,20);

[lua]
if ( CLIENT ) then

local on = false

local function toggle()

on = !on

if on == true then

	print( 'enabled' )
	
	LocalPlayer():PrintMessage( HUD_PRINTTALK, "Third person mode enabled." )

else

	print( 'disabled')
	
	LocalPlayer():PrintMessage( HUD_PRINTTALK, "Third person mode disabled." )

end

end

hook.Add( “ShouldDrawLocalPlayer”, “ThirdPersonDrawPlayer”, function()

if on and LocalPlayer():Alive() then

	return true

end

end )

hook.Add( “CalcView”, “ThirdPersonView”, function( ply, pos, angles )

if ( !on || !IsValid( ply ) || !ply:Alive() || ply:InVehicle() || ply:GetViewEntity() != ply ) then return end

	local view = {};
	local dist = 100;
	local trace = util.TraceHull( {
		start = pos,
		endpos = pos - angles:Forward() * 100,
		filter = { ply:GetActiveWeapon(), ply },
		mins = Vector( -4, -4, -4 ),
		maxs = Vector( 4, 4, 4 ),
	} )

	if ( trace.Hit ) then
	
		pos = trace.HitPos
	
	else pos = pos - angles:Forward() * 100
	
	end

	return {
		origin = pos,
		angles = angles,
		drawviewer = true
	}

end )

concommand.Add( “thirdperson_toggle”, toggle )

end

if ( SERVER ) then

function ulx.thirdperson( calling_ply )

calling_ply:SendLua([[RunConsoleCommand("thirdperson_toggle")]])	

end

local thirdperson = ulx.command( “Custom”, “ulx thirdperson”, ulx.thirdperson, {"!thirdperson", “!3p”}, true )
thirdperson:defaultAccess( ULib.ACCESS_ALL )
thirdperson:help( “Toggles third person mode” )

end[/lua]

I took a little bit of code out of Rubat’s Lightsaber Addon.

It works :slight_smile:

However, I’d still like to know about the tracehull…