Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

2 comments

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?

0

Oops I'll fix it

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.

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.