How to find if a player is in the vision?

Hi guys, I want to find the part that’s in the triangle! But my script found both.

local PartEnemy = workspace.Enemy
local AllParts = workspace.AllParts

while wait() do
	for i = 1, #AllParts:GetChildren() do
		local part = AllParts:GetChildren()[i]
		local vector = (PartEnemy.Position - part.Position)
		if vector.magnitude < 14 then
			local facing = PartEnemy.CFrame.LookVector
			local angle = math.acos(facing:Dot(vector.Unit))
			if angle >= math.rad(30) then
				print(part.Name)
			end
		end
	end
end

3 Likes

You can use:

It returns whether a Vector3 is currently in the users viewport.

I tried your code and what worked for me was to use one of the following changes:
local angle = math.acos(facing:Dot(-vector.Unit))
or change to the BackVector (Negative LookVector):
local facing = -PartEnemy.CFrame.LookVector
or change the order:
local vector = (part.Position - PartEnemy.Position)
and finally, you want to:
if angle <= math.rad(30) then

1 Like

Thank for answer, its not a bad ideas for players but a NPC doesnt have Camera!

Oh thank you for answer I try it later