Skip to content

Commit

Permalink
Improve raycast performance when expanding ignore list.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNexusAvenger committed Apr 14, 2024
1 parent 6e820c2 commit 7e53351
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/Util/FindCollidablePartOnRay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ local Workspace = game:GetService("Workspace")
Ray casts to find a collidable part.
--]]
local function FindCollidablePartOnRay(StartPosition: Vector3, Direction: Vector3, IgnoreList: Instance | {Instance}?, CollisionGroup: string?): (BasePart?, Vector3)
--Convert the collision group.
if typeof(CollisionGroup) == "Instance" and CollisionGroup:IsA("BasePart") then
CollisionGroup = CollisionGroup.CollisionGroup
end

--Create the ignore list.
local Camera = Workspace.CurrentCamera
local NewIgnoreList = {Camera}
--Clone the ignore list to prevent unexpected mutation of the list.
local NewIgnoreList = {Workspace.CurrentCamera}
if typeof(IgnoreList) == "Instance" then
table.insert(NewIgnoreList, IgnoreList)
elseif typeof(IgnoreList) == "table" then
for _, Entry in IgnoreList do
if Entry ~= Camera then
if Entry ~= Workspace.CurrentCamera then
table.insert(NewIgnoreList, Entry)
end
end
end

--Convert the collision group.
if typeof(CollisionGroup) == "Instance" and CollisionGroup:IsA("BasePart") then
CollisionGroup = CollisionGroup.CollisionGroup
end

--Create the parameters.
local RaycastParameters = RaycastParams.new()
Expand All @@ -41,19 +40,22 @@ local function FindCollidablePartOnRay(StartPosition: Vector3, Direction: Vector
RaycastParameters.CollisionGroup = CollisionGroup
end

--Raycast and continue if the hit part isn't collidable.
local RaycastResult = Workspace:Raycast(StartPosition, Direction, RaycastParameters)
if not RaycastResult then
return nil, StartPosition + Direction
end
local HitPart,EndPosition = RaycastResult.Instance, RaycastResult.Position
if HitPart and not HitPart.CanCollide and (not HitPart:IsA("Seat") or not HitPart:IsA("VehicleSeat") or HitPart.Disabled) then
table.insert(NewIgnoreList, HitPart)
return FindCollidablePartOnRay(EndPosition, Direction + (EndPosition - StartPosition), NewIgnoreList, CollisionGroup)
end
while true do
--Raycast and try again if the hit part isn't collidable.
local RaycastResult = Workspace:Raycast(StartPosition, Direction, RaycastParameters)
if not RaycastResult then
return nil, StartPosition + Direction
end
local HitPart,EndPosition = RaycastResult.Instance, RaycastResult.Position
if HitPart and not HitPart.CanCollide and (not HitPart:IsA("Seat") or not HitPart:IsA("VehicleSeat") or HitPart.Disabled) then
table.insert(NewIgnoreList, HitPart)
RaycastParameters.FilterDescendantsInstances = NewIgnoreList
continue
end

--Return the hit result.
return HitPart, EndPosition
--Return the hit result.
return HitPart, EndPosition
end
end


Expand Down

0 comments on commit 7e53351

Please sign in to comment.