Hello, i wanted to write script that detects if there are any players in area infront on me. I know how to check if they are close enough (i mean radius) but i don’t know how to check if they are in angle of my “pizza slice”. Could you help me?
https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToScreenPoint
https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToViewportPoint
I don’t understand this at all
Vector:Dot is it
local function AngleBetween(vectorA, vectorB)
return math.deg(math.acos(math.clamp(vectorA:Dot(vectorB), -1, 1)))
end
AngleBetween(Vector3.new(0,0,0),Vector3.new(0,0,1)) -- will be 90 degrees
Thanks!
It seems
- It detects angle not connected with player CFrame but the othe angle (i don’t understand between what is this angle)
- It returns only 0,90,180 deg angles.
Do you have any idea how to do this better/other way?
it uses dot so it detects vector directions rather than using orientation (cant really explain it)
Did u change the vectors inside the function?
make sure its not the as i did inside the example script
Here is an module i made a while ago
https://www.roblox.com/library/8543928110/InFov
InFovModule.IsInFov(Object : Instance, Position : Vector3, NormalId : Enum, FieldOfViewMax : Number)
You can use the Dot product to achieve this.
Here’s a quick visualization:
You can start by creating an empty table that lists all the players that are in the area.
local playersInFOV = {}
Get your own humanoidRootPart
And loop through all available players per frame.
local runService = game:GetService("RunService")
local localPlayer = game.Players.LocalPlayer
local character = localPlayer.Character or localPlayer:CharacterAdded:Wait()
local rootPart = character:WaitForChild("HumanoidRootPart", 20) --Get your RootPart
local playersInFOV = {}
runService.HeartBeat:Connect(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player ~= localPlayer and player.Character ~= nil then
-- Get the other player's humanoidRoot
local otherPlayerRoot = player.Character:FindFirstChild("HumanoidRootPart")
if otherPlayerRoot then
-- Get the unit vector between the otherPlayer and yourself
-- And get the look vector of yourself
local selfToOtherPlayer = (otherPlayerRoot.Position - rootPart.Position).Unit
local lookVector = rootPart.CFrame.LookVector
-- And you can easily get the dot product by:
local dotProduct = lookVector:Dot(selfToOtherPlayer)
-- Based on the visualization: we can get our FOV if the dot product is above 0.5
-- And lastly list them in your table of players in FOV
if dotProduct >= 0.5 then
if not table.find(playersInFOV, player) then
table.insert(playersInFOV, player)
end
else
-- Remove them if they are out of range
local check = table.find(playersInFOV, player)
if check then
table.remove(playersInFOV, check)
end
end
end
end
end
end)
Now you have your lists of players that are on your Field of View. You can continue from there on.
Hope this helps cheers!
Thanks man! You did explain it very clearly, i’m very thankful