I am following this tutorial about NPC eyesight and the last part is about checking if the NPC has detected any players or other objects. The math parts of it are the most confusing and the main part I am struggling at grasping
https://devforum.roblox.com/t/how-to-create-a-realistic-npc-eyesight-system/2456522
So uhhhh if you wanna give this a shot here is the code:
local function checkSight(npc)
local detectable = {}
local characters = getCharacters()
local taggedObjects = CollectionService:GetTagged("Detectable")
for index, character in characters do
table.insert(detectable, character)
end
for index, object in taggedObjects do
table.insert(detectable, object)
end
for index, object in detectable do
if object:IsA("Model") then
object = object.PrimaryPart
end
local headPosition = npc.Head.Position
local objectPosition = object.Position
local headCFrame = npc.Head.CFrame
local direction = (objectPosition - headPosition).Unit
local lookVector = headCFrame.LookVector
local dotProduct = direction:Dot(lookVector)
local angle = math.deg(math.acos(dotProduct))
local distance = (headPosition - objectPosition).Magnitude
if angle > fieldOfView then
continue
end
if distance > viewRange then
continue
end
if raycast(npc, headPosition, objectPosition) then
return
end
if object:IsA("Model") then
objectLastDetected = object.Parent
else
objectLastDetected = object
end
return true
end
end
Introduction Hello everyone! In this tutorial, I’m going to teach you how to create a realistic NPC eyesight system so that NPCs can see and detect players and other objects. For example, you could use this to make a guard detect a player. Steps Create a script in ServerScriptService and name it whatever you’d like. This script will cont...