Hello. I have two Raycast methods and I’m wondering which one works best performance-wise. The main objective is to make the ray ignore every part that is invisible or not CanCollide.
The Situation
These Raycast methods will run every time a gun is shot. Say we are firing an automatic weapon with 600 RPM, it would run this method every 0.1 seconds.
Method 1
This method uses a pierce system where if the hit object is transparent or not CanCollide, it fires another Ray at the hit position, respecting the original range given. This method does not use Blacklist Arrays.
local function RayCast(Source, Direction, Range)
local DirectionwMag = Direction * Range
local Result = workspace:Raycast(Source, DirectionwMag)
if not Result then
Result = {Position = Source * DirectionwMag}
elseif Result.Instance.Transparency >= 1 or not Result.Instance.CanCollide then
local Dist = (Result.Position - Source).magnitude
local newRange = Range - Dist
Result = RayCast(Result.Position, Direction, newRange)
end
return Result
end
Method 2
This method uses a module that will store every part that the ray should avoid, and will be used as a Blacklist Array.
BlacklistModule
local BL = {}
local function ProcessPart(part)
if part:IsA("BasePart") then
if part.Transparency >= 1 or not part.CanCollide then
if not table.find(BL, part) then
table.insert(BL, part)
end
end
end
end
for _,v in pairs(workspace:GetDescendants()) do
ProcessPart(v)
end
workspace.DescendantAdded:Connect(ProcessPart)
return BL
MainScript
local BL = require(BlacklistModule)
local function Raycast(Source, Direction, Range)
local param = RaycastParams.new()
param.FilterType = Enum.RaycastFilterType.Blacklist
param.FilterDescendantsInstances = BL
local DirectionwMag = Direction * Range
local Result = workspace:Raycast(Source, DirectionwMag, param)
if not Result then
Result = {Position = Source * DirectionwMag}
end
return Result
end
Again I’m asking which method is more better in terms of performance. Personally I think Method 2 would produce a lot of background lag while Method 1 may lag too if there are too many parts in the way of one direction.
- Method 1
- Method 2
0 voters