local delta = 0.15 -- the delta angle
local vectorpos = Vector3.new(0,0,0) -- the position of the detector's origin
local vectoraim = Vector3.new(0,0,1) -- the direction of the detector, should be a unit
local targetpos = Vector3,new(0,0,2) -- the position of the target point
local cosdelta = math.cos(delta);
local targetaim = (targetpos - vectorpos).unit -- the unit direction of the target (do not modify)
if vectoraim:Dot(targetaim) >= cosdelta then
print(target is within range)
end
Minor tweaks:
targetaim is adjusted to be the vector you travel to get from the source to the target (because that’s how vectoraim is defined). That is to say, I flipped the sign.
We compare the dot product against math.cos(delta) instead of delta.
Although this is a 2-month bump, I recently wrote some code to solve this and thought that it may be appreciated by future readers looking for the same kind of thing. I’ve optimized it as far as reasonably possible.
local Angle = math.pi * 0.5 -- Example: 90 degrees (whole angle)
local Check = 0.5 + math.cos(Angle) * 0.5 -- Avoid trig in function call
local Epsln = 1e-5
local function Query(Pos, Dir, QueryPos)
local Vec = QueryPos - Pos -- Subtraction
local Dot = Dir:Dot(Vec) -- Multiplication & addition
if Dot < 0 then -- QueryPos is behind Pos
return false
end
local Dst = Vec:Dot(Vec) -- Multiplication & addition
if Dst < Epsln then -- QueryPos coincides with Pos
return true
end
return Dot * Dot / Dst >= Check -- One division, no sqrt
end