I need help with using the Dot Product!

  1. What do you want to achieve? Keep it simple and clear!
    I wan’t to edit my dot product script so that players behind a wall/part aren’t detected.
  2. What is the issue? Include screenshots / videos if possible!
    My NPC has a script inside it that basically picks up players that enter its field of view.
    My problem is that even if the player is behind a wall, the bot still thinks its in its FOV.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried looking tutorials on YouTube/the Dev Forum but nothing helped.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
--ServerScript placed inside of the NPC's Humanoid
function lolWalk()
    local closestmane 
    local closesmaneMag = 1000000000000
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Character then
            local hrp = player.Character.Torso
            local hrpMagnitude = hrp and (script.Parent.Parent.Torso.Position - hrp.Position).Magnitude or math.huge
            if closesmaneMag >= hrpMagnitude then
                closesmaneMag = hrpMagnitude
                closestmane = hrp
    return closestmane

	while wait() do	
	repeat wait() until lolWalk()
	local torso = lolWalk()
	local npc = script.Parent.Parent
	local character = torso.Parent
	local npcToCharacter = (character.Head.Position - npc.Head.Position).Unit
	local npcLook = npc.Head.CFrame.LookVector
	local dotProduct = npcToCharacter:Dot(npcLook)
		if dotProduct > .5 then
			--Char in POV
            script.LookingAtPlayer.Value = true
			print("in pov")
			--Char isnt in POV
			script.LookingAtPlayer.Value = false
			print("not in pov")

All help is appreciated!

1 Like

You might need to raycast to the player and see if something is in the way. There are some changes to raycasting that were implemented recently so try not to use any outdated resources (within the last two months is fine).