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

How to detect if a part is within your field of view (Line of Sight)? [UNSOLVED]

Asked by 10 years ago

Is there any possible way to know whether a part is within your field of view? 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 field of view, that'd be nice to know! Thanks in advance!

EDIT: Perhaps the term "Line of sight" would be better instead of FOV? I want to detect if the player can see a part, that's all.

2 answers

Log in to vote
0
Answered by 10 years ago

Do this:

First make a localscript and put it in StarterGui then put this code in the localscript

Code:

local Camera = game.Workspace.CurrentCamera
local FOV = Camera.FieldOfView -- Field Of View :: INFO - Field Of View goes by radius
local Player = game.Players.LocalPlayer
local Parts = {}

function Scan(Parent)
    for i, v in pairs(Parent:GetChildren()) do
        table.insert(v)
        if #v:GetChildren() > 0 then
            Scan(v)
        end
    end
end

Scan(Workspace)

function GetPart(Player)
    for i=1, #Parts do
        if (Player.Character.Torso.Position - Parts[i].Position).magnitude < FOV then
            return Parts[i]
        end
    end
end

game:GetService("RunService").RenderStepped:connect(function()
    local Part = GetPart(Player)
    print(Part.Name)
end)

want to know more about magnitude and RunService click the links below:

Magnitude RunService

0
This doesn't work, because the Scan() method is never called, so the Parts array will always stay empty, and so the GetPart() method returns nothing. Am I supposed to call Scan() somehow? NutsNWaffles 135 — 10y
0
Oops I'll fix it DragonSkyye 517 — 10y
Ad
Log in to vote
-3
Answered by 10 years ago

Hmm....I've never heard of this, and my scripting isn't that advanced. But, I do have an idea. Try making a script that creates a part on the front part of your character that is attached to where your head is. And then make a script on what it needs to do, and then put that script into the part that is created via script.

0
Not only will that make your walking "derpy", it isnt quite a Field of View, since you're only going to detect things that are STRAIGHT AHEAD of your head, and not to the sides. NutsNWaffles 135 — 10y

Answer this question