# Calculating if a point is within a turret's range (cone)

I am working on a turret that shoots out a beam to the closest enemy in its ‘range’. For this, I want to make a table of all the enemies in range and then pick the closest one to aim at.

My question is, how would I calculate if a certain point is within a cone or not?

Is there a way to perhaps compare the angle between a Ray through point A and B and a Ray through point A and P1/2?

(For this problem, it is not 100% necessary to fit a cone, it can also be the following shape:)

1 Like

Yes, there is a way to compare these angles! You can use a Dot Product Vector3:Dot( Vector3 other ). Use the returned value in the equation:

cos(Θ) = (A_B • A_P1) / (|A_B| * |A_P1|)

``````    local vA_B = RAY VECTOR
local vA_P1 = P1 - A

local angle = math.acos((vA_B:Dot(vA_P1)) / (vA_B.Magnitude * vA_P1.Magnitude))
``````
1 Like

I’m not sure if this helps your situation, but I have a function that gets the angle between a part and a position

``````function CheckIfFacing(part,pos,checkAngle)
local Facing = part.CFrame.LookVector
local Vector = (pos - part.Position).Unit

local gotAngle = math.deg(math.acos(Facing:Dot(Vector)))
--print(gotAngle)
return gotAngle <= checkAngle
end
``````

You could then check if its within maybe 45?

7 Likes