How would I make the camera collide with the world with this third person code.

function MyCalcView(ply, pos, angles, fov)
local view = {}
view.origin = pos + angles:Forward()*-115 + angles:Up()*2 + angles:Right()*23
view.angles = angles
view.fov = fov

return view


hook.Add(“CalcView”, “MyCalcView”, MyCalcView)

hook.Add(“ShouldDrawLocalPlayer”, “MyHax ShouldDrawLocalPlayer”, function(ply)
return true
hook.Add(“HUDShouldDraw”, “hidehud”, hidehud)

function render_square()
local ply = LocalPlayer()
local trace = ply:GetEyeTrace()
local pos = trace.HitPos:ToScreen()
local gap = 5
local length = gap + 5

if ply:Alive() then

surface.SetDrawColor( 255, 0, 0, 255) --Red  

--draw the crosshair  
surface.DrawLine( pos.x - length, pos.y, pos.x - gap, pos.y )  
	surface.DrawLine( pos.x + length, pos.y, pos.x + gap, pos.y )  
	surface.DrawLine( pos.x, pos.y - length, pos.x, pos.y - gap )  
surface.DrawLine( pos.x, pos.y + length, pos.x, pos.y + gap )  

hook.Add(“HUDPaint”, “square”, render_square)
hook.Add(“HUDShouldDraw”, “hidehud”, hidehud)[/lua]

Come on people, we need help D:

we don’t want the camera to pass the boundaries of maps.

In the MyCalcView function check if the position is in the world with util.IsInWorld and if it isn’t just set it forward more or go back into 1st person view.

Use traces.

Perform a trace between the original view position and the calculated view position, and if it hits, set the new position to where the trace hit.

[lua]local view, tr
hook.Add( “CalcView”, “<insert unique name>”, function( ply, origin, angles, fov )
view.origin = origin + angles:Forward()*-115 + angles:Up()*2 + angles:Right()*23

tr = util.Traceline {
	start = origin;
	endpos = view.origin;
	filter = ply;

view.origin = tr.HitPos or view.origin
view.angles = angles
view.fov = fov

return view

end )[/lua]

Something like this.

[lua]view.origin = tr.HitPos or view.origin[/lua]

tr.HitPos alone is enough, it’s equal to the endpos if the trace doesn’t hit.


Your Lua writing style is also really confusing for begineers, and given that those guys are obviously new to Lua, you should explain what you’re doing so they don’t have trouble understanding what’s going on.


A lot of things are wrong in there too, here’s a fixed version:

[lua]hook.Add( “CalcView”, “MyCalcView”, function( ply, pos, angles, fov )
local newpos = pos + angles:Forward()*-115 + angles:Up()*2 + angles:Right()*23

local tr = util.TraceLine {
	start = pos;
	endpos = newpos;
	filter = ply;

return {
	origin = tr.HitPos;
	angles = angles;
	fov = fov;

end )[/lua]

Something good to know, in Lua tables, semicolons (:wink: and commas (,) are exactly the same. You can use semicolons when you want to write a table over several lines, for readability purposes, it looks much cleaner that way.

All those lines of code are valid and do the same thing:

[lua]tab = {1, 2, 3}

tab = {1, 2, 3, }

tab = {1; 2; 3}

tab = {1; 2; 3;}

tab = {

tab = {

Also, when a function takes only one parameter, and that parameter is either a string or a table, you can omit the parentheses.

[lua]print “hello world”
PrintTable{1, 2, 3}[/lua]

That’s always good to know. :v:

I’m aware of this, I just keep forgetting :eng99:
I blame Deco for my style! :ohdear:

Never mind I got it working.

Thanks guys :slight_smile: