NPC - Dot Product Confusion

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer

repeat task.wait() until player.Character or player.CharacterAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()

local Humanoid = character:WaitForChild("Humanoid")
local HRP = character:WaitForChild("HumanoidRootPart")
local Head = character:WaitForChild("Head")

local Looking_NPC = workspace:WaitForChild("Looking_NPC")
local NPC_HRP = Looking_NPC:WaitForChild("HumanoidRootPart")
local NPC_Head = Looking_NPC:WaitForChild("Head")

local Bounding_Part = workspace:WaitForChild("Bounding_Part")

local OverlapParameters = OverlapParams.new()
OverlapParameters.FilterType = Enum.RaycastFilterType.Exclude
OverlapParameters.FilterDescendantsInstances = {Bounding_Part, Looking_NPC, workspace.Lobbies.Secondary_Lobby.Baseplate}

local function Check_Player(Parts_List)
	local Found_Player = false
	
	for i, Part in Parts_List do
		if Part.Name == "Handle" and (Part.Parent:IsA("Accessory") or Part.Parent:IsA("Tool")) and Players:GetPlayerFromCharacter(Part.Parent.Parent) or Players:GetPlayerFromCharacter(Part.Parent) then
			Found_Player = true
		end
	end
	
	return Found_Player
end

while true do
	local Parts_In_Bound = workspace:GetPartBoundsInBox(Bounding_Part.CFrame, Bounding_Part.Size, OverlapParameters)
	print(Parts_In_Bound)
	
	local Found_Player = Check_Player(Parts_In_Bound)
	
	local Dot_Product = NPC_HRP.CFrame.LookVector:Dot(HRP.CFrame.LookVector)
	print(Dot_Product)
	
	if Found_Player == true then
		if Dot_Product >= 0.5 then
			print("Player is within the NPC's FOV!")
			local LookAt_CFrame = CFrame.lookAt(NPC_Head.Position, Head.Position)
			local X, Y, Z = LookAt_CFrame:ToOrientation()
			
			TweenService:Create(NPC_Head, TweenInfo.new(0.2, Enum.EasingStyle.Linear), {Orientation = Vector3.new(math.deg(X), math.deg(Y), math.deg(Z))}):Play()
		else
			print("Player is not within the NPC's FOV!")
		end
	else
		print("Didn't find player!")
	end
	
	task.wait(1)
end

Hello! I’m trying to practice :GetPartBoundsInBox() and Dot Products and I was trying to practice them by attempting to create an NPC that looks at a player whenever the player is in the box and within the NPC’s FOV range. However, I don’t understand why this isn’t working and I think it should? Can anyone help me with this and Dot Products?

You’re using the dot product of the NPC’s LookVector and the your character’s LookVector, which isn’t what you want to do if you want to determine if you’re in front of or behind the NPC.
You need to use the dot product of the NPC’s look vector and the unit vector between you and the NPC instead.

local toCharacter = (HRP.Position - NPC_HRP.Position).Unit
local dot = toCharacter:Dot(NPC_HRP.CFrame.LookVector)
1 Like

Thank You! It worked! Is there anything that I should know about Dot Products and :GetPartBoundsInBox()?

Using the dot product here gives you a number between -1 and 1, with -1 being exactly behind the NPC, and 1 being exactly in front of the NPC. This number can be plugged into acos() to get an angle in radians, which allows you to more finely tune the field of view if you’d like.

--fieldOfView assumed to be in degrees
local function isInFieldOfView(fieldOfView : number, dotProduct : number) : boolean
    local angle = math.acos(dotProduct)
    return angle <= math.rad(fieldOfView)
end

Oh alright, I’ll try it out! :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.