✓ Accepted answer
Here's a Local Script to detect, if Descendants of a Model is in, player's view.
In the script it tells you where you put the code for when "monster is being seen" or when "monster is not being seen"
Look below the script to get a short explanation of how it works.
Also, if this helps, then, please accept my answer. It would be appreciated. :)
Script:
local monsterCharacterModel = nil
local function noObstructs(position,character)
local castPoints = {workspace.CurrentCamera.CFrame.Position,position}
local ignoreList = {game.Players.LocalPlayer.Character, character}
local obstructs = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints,ignoreList)
if obstructs[1] then
elseif not obstructs[1] then
end
end
local function ViewPointCheck()
for i,v in pairs(monsterCharacterModel:GetDescendants()) do
if v:IsA"Part" or v:IsA"MeshPart" then
local part,bool = workspace.CurrentCamera:WorldToScreenPoint(v.Position)
if bool then
noObstructs(v.Position,v.Parent)
elseif not bool then
end
end
end
end
game:GetService("RunService").Heartbeat:Connect(ViewPointCheck)
The Local Script uses Heartbeat Event to constantly call the function ViewPointCheck. The Function ViewPointCheck checks if every Part and MeshPart of monsterCharacterModel's Position is Visible within the Camera's View. And if Visible is true then it call the function noObstructs. The Function noObstructs checks if there are any parts blocking two points. Which the two points are the Camera and the Visible Part. If there are obstructs then the part is not physically visible. If there are no obstructs then the part is physically visible.
4 comments
you could use dot products to make a field of sight
Can you elaborate?
Is this a multiplayer game? What if multiple players are not seeing it? Who is to be chased?
The nearest person would be chased. If one person looks at it then it would stop moving for all clients. I'm making this for both single and multiplayer games. Sorry if i didn't explain enough im on mobile right now