Detect if an Object is within the specific view of player

I wish to find if an object is within the specified view of the direction the player is facing.

Example:
image

How would I go about doing this?

You can do that with raycast: Raycasting | Roblox Creator Documentation

Could you explain it a little further? How would I check if it’s in the specific view? And does raycast work if there’s a wall in the way?

You can get the lookVector of the player character.

Yes

The object I have will be moving. Would I just have to cast a new ray every {interval}

To give some back story, this is for a bounty tracking system. A player has a tracker tool, that will update on the players location and if the player is still within the specified view of the user, it’ll ping, if not, it won’t.

Nope, I don’t think so. (Technically, you have to re-raycast for everytime the player moves.) Let me write a quick script you can test

local player = script.Parent
local TS = game:GetService("TweenService")

game:GetService("RunService").RenderStepped:Connect(function()
	local hitPart = workspace:Raycast(player:FindFirstChild("HumanoidRootPart").Position, player:FindFirstChild("HumanoidRootPart").CFrame.LookVector * 15)
	if hitPart then
		print(hitPart.Material)
	end
end)

https://gyazo.com/dd9ce515b863597d397d0755b2f96bc5
You can increase range if you want too, by changing the * 15

So, in order to make it an interval since I do not want instant updates, I’d just run the raycast once every few seconds?

Waittt… I’ve just looked over the Gyazo. This is not the kind of thing I’m looking for. I’m searching for a specific part, not just any part that is infront of me

Well, you can do an if conditional for that:

local player = script.Parent
local TS = game:GetService("TweenService")

game:GetService("RunService").RenderStepped:Connect(function()
	local hitPart = workspace:Raycast(player:FindFirstChild("HumanoidRootPart").Position, player:FindFirstChild("HumanoidRootPart").CFrame.LookVector * 15)
	if hitPart:IsA("BasePart") and hitPart.Name == "blabla" then -- check name
		-- do stuff
	end
end)

Yes, but as I stated before, would this work if the part I’m searching for is behind a wall?

They are working on implementing canquery for raycasts soon.
Workaround for right now is that you can whitelist the specific parts using RaycastParams:

local player = script.Parent
local TS = game:GetService("TweenService")


local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {game.Workspace.Query}
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist

game:GetService("RunService").RenderStepped:Connect(function()
	local hitPart = workspace:Raycast(player:FindFirstChild("HumanoidRootPart").Position, player:FindFirstChild("HumanoidRootPart").CFrame.LookVector * 45, raycastParams)
	if hitPart then
		print(hitPart.Material)
	end
end)

You can put every part supposed to be scanned in a folder and do:
raycastParams.FilterDescendantsInstances = {game.Workspace.Folder:GetChildren()}

Hello!

I would suggest you use Dot product for something like this

Here is a tutorial about it here,

And if you need an example code you can check out this reply on a previous thread! :slight_smile:

This code will detect the part anyways even if its behind a wall, if you don’t want that then you will have to raycast from the player to the part to see if is there something on the way

1 Like

This isn’t working. I’m unsure as to why.

You have to edit the code to make it what you want it to do first

local hrp = game.Workspace.Dummy.HumanoidRootPart -- something idk im writing this quick
local hrp2 = game.Workspace.LookToPart
local vector = hrp.Position - hrp2.Position
local magnitude = vector.Magnitude
local radius = 50 -- change to what you want
local angle = 45 -- threshold for the angle

if magnitude <= radius then
	local dot = hrp.Position:Dot(hrp2.Position)
	if dot >= 0 and dot <= math.cos(math.rad(angle)) then
		print("found it!!")
	else
		print("didnt find it")
end
end

This is my code.
It is returning “didn’t find it” when the part is right infront of the HRP.

Did you try this? @FeudalLucy 3

I did. It also did not work. Unsure as to why.

You should use the lookVector instead of hrp.Position in the dot product.

This is because in your current code, it will calculate an angle using a direction vector from the origin to hrp instead of where the player is facing, and thus the wrong angle.

Here’s some other basic code you can manipulate if it helps you understand it better.

local v1 = lookVector
	local v2 = head.Position - target.Position
	
	local dp = v1:Dot(v2)
	local mags = v1.Magnitude * v2.Magnitude
	local cos = dp/mags
	local adjCos = math.clamp(cos, -1, 1)
	local theta = math.acos(adjCos)
	local angle = math.deg(theta)

Would using it in a server-script affect it? (I removed the renderstepped.)