Detect if an Object is within the specific view of player

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.)

Ah, I don’t know what does your script look like? It worked perfectly for me as you can see in the image

I found more information below the reply on the thread you could try this one too

local hrp = plr.HumanoidRootPart -- something idk im writing this quick
local hrp2 = plr2.HumanoidRootPart
local vector = hrp.Position - hrp2.Position
local magnitude = vector.Magnitude
local radius = 3 -- change to what you want
local angle = 45 -- threshold for the angle

  local dot = hrp.Position:Dot(hrp2.Position)
  if dot >= 0 and dot <= 0.707 )
   if magnitude <= radius then
      -- code that you want to do after you detect
   end
 end

Just edit it to your version of the code and try it out, sorry for the bad formatting, I am on mobile

No, but you need to raycast again every 0.1 or so seconds so it updates, btw I forgot to tell you but I recommend the script should be a startercharacterscript (or you can do .characteradded)

So I’d check if the dp was greater than 0, and if the angle was >= 45?

Checking the dot product would be unnecessary at that point, if it is negative then the angle is greater than 90 degrees which checking the angle itself would prevent anyway.

You would just have to make sure that angle <= 45, which would mean that it is within the view wanted.

Figured it out. With a little testing, checking if angle > 90 is the correct view I require. Thanks!

1 Like