I haven’t created very many hit detection systems before, but I’m looking to start out of semi-necessity, because me and a group of others are working on a game and it requires guns.
Here’s my problem: I plan do this on the server where many bullets will be handled, probably across many threads (multithreading maybe necessary because the function I’m using naturally yields…?). Something tells me that this isn’t exactly the greatest solution in terms of performance. In fact, I forsee gamebreaking lag if more than 3 people start shooting rapidly at once.
Am I correct in concerns about performance? If so, does anyone have any suggestions on how to make this more performant?
Here’s the code:
local DefaultSpeed = 100
local Bullet = {}
function Bullet.fire(Origin, Direction, StartTick, IgnoreList)
local PreviousTick = StartTick
local Result
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = IgnoreList
Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.IgnoreWater = false
repeat
Result = workspace:Raycast(
Origin,
Direction.Unit * DefaultSpeed * (tick() - PreviousTick),
Params
)
PreviousTick = tick()
until Result
return Result
end
return Bullet