NPC see check

How to check if NPC can see it’s enemy or if enemy see NPC?

Give one of your npcs a weapon if the npc kills the other one it means it saw it.

You can probably use a trace.

Trace from the NPC’s view to the target NPC

Like this?



local pos = self:GetPos()
local ang = (self.Enemy:GetPos() - self:GetPos()):Angle()
local tracedata = {}
tracedata.start = pos
tracedata.endpos = pos+(ang*5000)
tracedata.filter = self
local trace = util.TraceLine(tracedata)
if trace.HitNonWorld then
	target = trace.Entity
	if(target == self.Enemy) then
		print("TARGETED")
		self.Sees = 1
	else
		self.Sees = 0
	end
end	


It hardly work because TARGETED message pop-ups randomly.

EDIT2: solved by: tracedata.endpos = self.Enemy:GetPos()

Something like this should work
[lua]
function CanNPCSeeEntity(npc, target)
local dir = (target:GetPos() - npc:GetPos()):Normalize()
local tr = util.TraceLine({start = npc:GetPos(), endpos = npc:GetPos() + dir * 16384, filter = npc})
return dir:Dot(npc:GetAimVector()) >= 0.5 and tr.Entity == target
end
[/lua]