How to check if there are any player infront on me

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?

I don’t understand this at all :confused:

Vector:Dot is it

local function AngleBetween(vectorA, vectorB)
	return math.deg(math.acos(math.clamp(vectorA:Dot(vectorB), -1, 1)))

AngleBetween(,0,0),,0,1)) -- will be 90 degrees

It seems

  1. It detects angle not connected with player CFrame but the othe angle (i don’t understand between what is this angle)
  2. 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

1 Like

Here is an module i made a while ago

InFovModule.IsInFov(Object : Instance, Position : Vector3, NormalId : Enum, FieldOfViewMax : Number)
1 Like

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 = {}

  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)
               -- Remove them if they are out of range
               local check = table.find(playersInFOV, player)
               if check then
                   table.remove(playersInFOV, check)

Now you have your lists of players that are on your Field of View. You can continue from there on.

Hope this helps cheers!

1 Like

This is a really good video explaining how the dot product works


Thanks man! You did explain it very clearly, i’m very thankful :slight_smile:

1 Like