Hello!
This is a simple module I made for my own purposes. I thought I would open-source it so that others may use it as well.
What it does
This generates a cylinder from raycasts to detect if anything is colliding.
If something is colliding, you will be given a RaycastResult
.
Source code
This is a module script.
local RadialRaycastCheck = {}
type array<t> = {[number]:t} -- Array variable type
type dictionary<t> = {[string]:t} -- Dictionary variable type
local RaycastPar = RaycastParams.new()
RaycastPar.FilterType = Enum.RaycastFilterType.Blacklist
local PropertyChecks = { -- If any of these are true during the property check, it is ignored in collision
CanCollide = false,
Transparency = 1,
}
local function CreateCylinderArray(Position: CFrame, Height: number) -- math :c
local PositionsArray: array<CFrame> = {}
local BasePosition = CFrame.new(Position.X, 0, Position.Z)
for PosY = Position.Y-Height/2, Position.Y+Height/2, Height/2 do
for Rotation = 0, math.pi*2, math.pi/30 do
local NewCFrame = BasePosition * CFrame.new(0, PosY, 0) * CFrame.fromEulerAnglesXYZ(0, Rotation, 0)
table.insert(PositionsArray, NewCFrame)
end
end
return PositionsArray
end
function RadialRaycastCheck:Check(Position: CFrame, Radius: number, Height: number, Ignore: array<any>)
local NewCylinder = CreateCylinderArray(Position, Height)
RaycastPar.FilterDescendantsInstances = Ignore
local CollisionResult = nil -- Will be a RaycastResult
for _, CheckCFrame: CFrame in pairs(NewCylinder) do
local RaycastResult = workspace:Raycast(CheckCFrame.Position, CheckCFrame.LookVector * Radius, RaycastPar)
local RaycastInstance = RaycastResult and RaycastResult.Instance
if RaycastInstance then
local DidCollide = true
for Property, Value in pairs(PropertyChecks) do
if RaycastInstance[Property] == Value then
DidCollide = false
break
end
end
if DidCollide then
CollisionResult = RaycastResult
break
end
end
end
return CollisionResult
end
return RadialRaycastCheck
How to use it
It’s simple to use!
- Get the module via
require
- To raycast radially, do
module:Check(Position, Radius, Height, IgnoreList)
And that’s it! It will return either a RaycastResult
or nil
.