How do i make a simple dotproduct script?

so how do i make it like, so it prints if target is in angle??

so if the target is in the field of view you mean?

yea

e

30303030chaaaaaaaaaaaaaaasr

Try this, put it in a heartbeatloop and change the variables to fit your situation

-- Variables
local NPC = script.Parent -- The NPC to detect
local Player = game.Players.LocalPlayer
local DetectionDistance = 10 -- The distance to detect the player
local DetectionAngle = math.rad(90) 

-- Function to detect player
function detectPlayer()
	local NPCForward = NPC.Head.CFrame.lookVector
	local NPCtoPlayer = Player.Character.Head.Position - NPC.Head.Position
	local NPCtoPlayerDirection = NPCtoPlayer.unit
	
	-- Calculate the dot product between the NPC's forward direction and the direction to the player
	local dotProduct = NPCForward:Dot(NPCtoPlayerDirection)
	
	-- Check if the player is within the field of view
	if dotProduct > math.cos(DetectionAngle) and NPCtoPlayer.magnitude <= DetectionDistance then
		print("Player detected!")
	else
		print("Player not detected.")
	end
end
1 Like

no i actually mean a part seeing a part ;_; (in a specific range of view)

Okay try this modified version

-- Variables
local Part1 = -- Part1 path
local Part2 = -- Part2 path
local DetectionDistance = 10 -- The distance to detect the part2
local DetectionAngle = math.rad(90) 

-- Function to detect player
function detectOtherPart()
   local Part2_Forward = Part1.CFrame.lookVector
   local Part2ToPart1 = Part2.Position - Part1.Position
   local Part2toPart1Direction = Part2ToPart1.unit

   -- Calculate the dot product between Part1's forward direction and the direction to the Part2
   local dotProduct = Part2_Forward:Dot(Part2toPart1Direction)

   -- Check if the Part2 is within the field of view
   if dotProduct > math.cos(DetectionAngle) and Part2ToPart1.magnitude <= DetectionDistance then
      print("Part2 detected!")
   else
      print("Part2 not detected.")
   end
end

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