Getting all players in another player's view?

I have been looking for a function to find all entities in a players view, but it doesn’t seem that one is implemented into the game. My question is, how would I go about writing one myself?

I have taken a look at Player:GetShootPos() and Player:GetAimVector(), but they would only provide everything directly under the player’s crosshair.

I thought of using ents.FindInCone(), but I’m not sure about how I’d go about using it with Player:GetAimVector().

Any ideas on doing this?


I tried the following, but got some weird results (seems to be 90 degrees offset?)

function GetEntsInView(ply)
	local eye_pos = ply:EyePos()
	local aim = ply:GetAimVector()
	local size = 35

	return ents.FindInCone(eye_pos, aim, size, 0)

I just made the ‘size’ value up, but in the end I guess it will use something such as the player’s FOV.

FindInCone is so ridiculously expensive to run, I wouldn’t use it unless you absolutely need to.

I used to use FindInCone for my light-render script. It’d cut FPS by 40 at times, even without vehicles spawned in the server, it’s too expensive to run.

The better thing would just to take the list of all players and see if you can draw a line to them without going through a wall. That’s less costly that findInCone.

[lua]local ents = { };

for _, v in pairs( player.GetAll() ) do

if( ( v:GetPos() - ply:GetPos() ):GetNormal():DotProduct( ply:GetAimVector() ) < 0.2588 ) then
	local trace = { };
	trace.start = ply:EyePos();
	trace.endpos = v:EyePos();
	trace.filter = ply;
	local tr = util.TraceLine( trace );
	if( tr.Entity and tr.Entity:IsValid() and tr.Entity == v ) then
		table.insert( ents, v );



return ents;[/lua]

Untested and you’ll probably have to play with the dot product line a lot (0.2588 is the cos of 75 degrees, or the HL2 default FOV).

Turns out that Entity:IsVisible works fine with players, but thanks for the help! :slight_smile: