Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i make sector radius detection?

Asked by 5 years ago

I know i can use these codes to make circle detect.

local center = Vector3.new(0,0,0) -- Center of area
local radius = 80

while wait() do
    if (center-games.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude <= radius then
    print("Player is inside area")
    end
end

But how about sector detect?
I'm not really want use touch detect,But if this is the only answer,I'll take that.
Thanks for answering!

0
I believe you are looking for this: http://wiki.roblox.com/index.php?title=API:Region3 mattscy 3725 — 5y
0
Wow this looks cool! but i need know where's my "space". MEndermanM 73 — 5y
0
Then how do i make sector shape? MEndermanM 73 — 5y
0
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 T0XN 276 — 5y
0
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. T0XN 276 — 5y

2 answers

Log in to vote
0
Answered by
ozzyDrive 670 Moderation Voter
5 years ago
Edited 5 years ago

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

--  You most likely have a known radius and thus this should be unnecessary
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

Ad
Log in to vote
0
Answered by 5 years ago

Though you do not want to use touch detect, it is indeed recommended and much much easier.

0
Why is touch events the best way to go about this? Please provide context. T0XN 276 — 5y

Answer this question