✓ Accepted answer
This is down to rather simple vector math. You can find a step-by-step guide from an answer to a very similar stackoverflow question. Here's an example on how it can be implemented on Roblox:
local function areClockwise(v1, v2)
v1, v2 = Vector2.new(v1.X, v1.Z), Vector2.new(v2.X, v2.Z)
return v1:Cross(v2) > 0
end
local function isWithinRadius(point, radius)
return Vector2.new(point.X, point.Z).Magnitude <= radius
end
local function isInsideSector(point, origin, sectorStart, sectorEnd, radius)
return not areClockwise(sectorStart, point) and areClockwise(sectorEnd, point) and isWithinRadius(point, radius)
end
local origin = workspace.origin.Position
local startPoint, endPoint = workspace.point1, workspace.point2
local startVector, endVector = (startPoint.Position - origin), (endPoint.Position - origin)
local radius = (startVector.Magnitude + endVector.Magnitude) / 2
local player = game.Players.PlayerAdded:Wait()
local character = player.CharacterAdded:Wait()
local primaryPart = character.PrimaryPart
game:GetService("RunService").Heartbeat:Connect(function()
local point = (primaryPart.Position - origin)
local isInside = isInsideSector(point, origin, startPoint.Position - origin, endPoint.Position - origin, radius)
local brickColor = isInside and BrickColor.Green() or BrickColor.Red()
startPoint.BrickColor, endPoint.BrickColor = brickColor, brickColor
end)
There are certain limitations mentioned in the SO answer.
EDIT: Fixed the link to point to the actual answer
5 comments
I believe you are looking for this: http://wiki.roblox.com/index.php?title=API:Region3
Wow this looks cool! but i need know where's my "space".
Then how do i make sector shape?
Take a look at this, Mighty_Loopy provides an effective explanation for using euler angles of CFrame to limit the rotation of a head that "follows" the player in regard to their orientation around the NPC: https://devforum.roblox.com/t/making-an-npc-look-at-your-character/114425/3
I can see how this is somewhat related to your situation, as the moving head script clamps the range in front of the NPC so that their head only turns if the player is in a certain range that is in front of the NPC.