Making a raycast touch detection system with Region3?

Before any replies, I won’t be trying to use any public module like Zones in a preference to create my own approach that I can customize to my specific need, I can potentially make it even more efficient depending on my use case.

I want to create a custom touched system for a group of objects much like the Zones module does and my objects are particularly circle objects.

There are flaws to doing the Touched and GetTouchingParts method that are great enough I want to do alternative approaches. I definitely can’t do magnitude calculation or only Region3 since I am using circular multiple objects.

So, I am trying to do it exactly how the Zone module states they do it, however, I am stuck at the raycast part.

Here is my proposed way of the raycasting method where I try to get the exact ground level the player is on then add 0.5 studs then shoot downward by a unit vector.

local function getObjectBelow(character)
    local humanoid = character.Humanoid
    local hrp = humanoid.RootPart
    local ray = Ray.new(hrp.Position - Vector3.new(0, hrp.Size.Y/2, 0) - humanoid.HipHeight + 0.5, -hrp.UpVector)
    return workspace:FindPartOnRay(ray, character) -- ignore character
end

I know about RaycastParams but for now I just had this before I knew about it so I’ll convert it to RaycastParams API use myself from what is given now.

Is this a good way to go about it or can/should I use a different approach?

1 Like

Magnitude is perfect for circular zones, and you can even do it with a zone that’s made out of multiple circles

Firstly, you could check if you’re in a circular area by ignoring the Y axis and comparing the Magnitude to the radius of the area:

local Diff = CircleCenter - CharacterPosition
local FlattenedDiff = Vector3.new(Diff.X, 0, Diff.Z)
local Distance = FlattenedDiff.Magnitude

if Distance <= CircleRadius then
    print("Character is in Circle")
end


Now, if your zone is made up of multiple circles, then you are in the zone if you are in at least one of the circles


(Each blue circle represents a different part of the Blue zone)

All you need to do is loop through your circles and check each of them until you find that you are in one of them or you run out of circles to check, meaning you are not in the zone.

2 Likes

I’ll give it a try when I can but I’d like to ask what a different approach would be were I not using only perfectly sized circles.