Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I detect whether a player can see an object or not?

Asked by 10 years ago

Is there any possible way to know whether a part is within your line of sight? I have looked around in the Wiki, and I haven't found anything of the sort... So if there is any way to create a method that can detect objects within your sight (detect whether the player can see the object or not), that'd be nice to know! Thanks in advance!

1 answer

Log in to vote
0
Answered by 10 years ago

Here I'll help u out

Btw this was done by IPad so sorry if there were any errors or simple mistakes

Make a local script put In startergui

local Player = game.Playera.LocalPlayer
local Camera = Workspace.CurrentCamera
local FOV = Camera.FieldOfView -- FOV = FieldOfView
local Parts = {} -- leave empty if u don't know what u are doing

Scan = function(object)
  for i, v in pairs(object:GetChildren()) do
   table.insert(Parts, v)
   if #v:GetChildren() > 0 then
    Scan(v) -- scan the object since it has children
  end
 end
end

Scan(Workspace) -- this will scan everything in the workspace

function getNearestObject()
   for i, v in pairs(Parts) do
      if (Player.Character.Torso.Position - v.Position).magnitude < FOV then -- magnitude gets the distance between the 2 points
        return v
      end
   end
end

while wait() do -- continuos loop
   local object = getNearestObject()
   print(object.Name) -- objects name goes towards output
end
0
This doesn't work, and I don't see how it should work. The if statement that checks whether the distance between the player and the object is greater than your FOV wont do what I asked for in my question. That statement would return true even if objects were behind me. I want to have objects that you can SEE, not that are in a certain range from you. (The game is always in first person state, by t NutsNWaffles 135 — 10y
0
Oops sorry change the greater to a less than DragonSkyye 517 — 10y
Ad

Answer this question