Created
July 12, 2023 10:08
-
-
Save cyrus01337/ee76a1a676d834000b6c2be376f05fd2 to your computer and use it in GitHub Desktop.
Function to raycast and get back multiple instances the raycast skewers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Parts = {BasePart} | |
local function findTopmostModelAncestor(instance: Instance): Instance? | |
local modelAncestorFound = instance:FindFirstAncestorOfClass("Model") | |
while modelAncestorFound do | |
local newModelAncestorFound = instance:FindFirstAncestorOfClass("Model") | |
if modelAncestorFound and newModelAncestorFound == nil then | |
return modelAncestorFound | |
end | |
modelAncestorFound = newModelAncestorFound | |
-- debugging | |
print("findTopmostModelAncestor") | |
task.wait() | |
end | |
return modelAncestorFound | |
end | |
local function findPartsInRay(origin: Vector3, direction: Vector3): | |
local partsFound: Parts = {} | |
local ignoreList: Parts = {} | |
repeat | |
local raycastParams = RaycastParams.new() | |
raycastParams.FilterDescendantsInstances = ignoreList | |
raycastParams.IgnoreWater = true | |
local resultFound = workspace:Raycast(origin, destination, raycastParams) | |
if not resultFound then continue end | |
local hit = resultFound.Instance | |
table.insert(partsFound, hit) | |
local partBelongsToModel = findTopmostModelAncestor(hit) | |
if partBelongsToModel then | |
table.insert(ignoreList, partBelongsToModel) | |
else | |
table.insert(ignoreList, hit) | |
end | |
-- debugging | |
print("findPartsInRay") | |
task.wait() | |
until resultFound == nil | |
return partsFound | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment