Detect when player is in front of you

What would be the best way to detect if a person is in a certain range and radius in front of you.

3 Likes

This is probally what you want.

But better look down more in Roblox Developer Api Reference or here (Roblox Forum), sure it will help.

Is there a way to do this with something like magnitude not a big fan of raycasting

Raycasting would be the way to go here, although if you really want to do this with magnitude then you could compare every player’s HumanoidRootPart position with a position offset a few studs in front your character’s HumanoidRootPart

Okay I’ll look try it with raycasting

Hello,im the person from the post you mention I found a solution

local Enemy = --place enemy here
    if ((EnemyChar.Character.PrimaryPart.CFrame*CFrame.new(0,1,-5).p)-PlrChar.PrimaryPart.Position).magnitude <= 10 then --Infront
    
    else --Behined
    
end

basically what it does it calculate if you are in the range infront of a player with magnitude

Dunno why people haven’t mentioned this, but you could do a magnitude check to see whether the other player is within the radius and then use the dot product between the vectors to see whether the player is facing their direction (within a certain threshold).

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

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

Yes I did my research and found that checking the magnitude and dot product was the most efficient way. Here are the two videos I found helpful if someone is trying to do this. Vector3 Dot Product: Calculating NPC Field of View (Roblox Studio) - YouTube
Find Distances Between Positions! - Roblox (Vector3 and Magnitude) - YouTube

2 Likes

It might be less computationally expensive to first check if the other player is in the viewing angle of the player before checking the magnitude between them.

1 Like

Magnitude is calculated as sqrt(a^2 + b^2). I’m not sure on this one.

Dot product doesnt have a sqrt root. Also you can void of the cosine function too. Here would be the updated code.

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

I used the cosine function to make it easy to switch the angle threshold by changing the variable.

Yea I realize, I was just pointing out that its not completely necessary.