local myents = ents.GetAll( )
local pos = LocalPlayer( ):EyePos( ) or ply:EyePos( )
local detectionRange = 1000
local index = LocalPlayer( ):EntIndex( ) or ply:EntIndex( )
for k,v in ipairs( myents ) do
local tr = util.TraceLine( {
start = v:EyePos( ),
endpos = pos,
filter = { } )
if IsValid( tr.Entity ) and tr.Entity:IsPlayer( ) and v:EyePos( ):Distance( pos ) <= detectionRange and tr.Entity:EntIndex( ) == index then
//attack code goes here
end
end
Do you want to know if the NPC's "see" the player, as far as engine ai goes? or literally if the player should be visible to the NPC?
Geohavic's code literally checks if there's a clean line between an entity's eyes and a player's eyes.
If the player/npc's eyes are blocked by a wall, a low hanging shelf, a flying NPC or prop, etc... even if an NPC can see a player's body or arms, that code says it can't see the player at all. I'm not sure if there's an *efficient* way to check every body part for visibility.
There is this though: Entity/Visible
Sorry, you need to Log In to post a reply to this thread.