Hi! I made my own raycasting hit module similar to @TeamSwordphin’s, and wanted to make sure that this was efficient and i’m not doing any unnecessary tasks that could slow down performance. This is the main section of the script that does the detection.
How it works is it generates a raycast from every attachment on a heartbeat event. The debug rays are not important since it will mostly be switched off.
Here’s a video on how it works:
https://gyazo.com/95993502310dd966a02730abc4d8959f
function RAYCACHE:BeginTracking() -- Start Raycasting
local previousPositions = {} -- Save every attachment's last position here
local cloned_WhiteList = self.Whitelist -- Make a duplicate of the global whitelist so we can remove hit humanoids
-- add current positions of attachments
for _, attachment in pairs(self.Attachments) do
previousPositions[attachment] = attachment.WorldPosition
end
-- start casting rays
self.Connection = RunService.Heartbeat:Connect(function(dt)
-- loop through every attachment in blade
for attachment, lastPosition in pairs(previousPositions) do
local cast_ray = ray(lastPosition, attachment.WorldPosition - lastPosition) -- cast ray from last position to new position
local collected = Workspace:FindPartOnRayWithWhitelist(cast_ray, cloned_WhiteList, true) -- find hit parts from ray
if collected then -- if any parts found, remove it from the duplicate whitelist so we don't damage the humanoids more than once
remove(cloned_WhiteList, collected.Parent)
end
-- debug stuff, not important
if DEBUG_RAYS then
RAYCACHE.GenerateDebugRay(lastPosition, attachment.WorldPosition)
end
-- update lastPosition in previousPosition table
previousPositions[attachment] = attachment.WorldPosition
end
end)
end
Appreciate any feedback.