Making new Ray cast Params often question

If I’m making bullet penetration and I need a bullet to ignore the part it just hit, Is it laggy or inefficient to make new raycast parameters every time just to ignore this part?

You could dynamically change the FilterDescendantsInstances property of the RaycastParams object and reuse it.

RaycastParams.FilterDescendantsInstances = {firstHitPart}
--reuse raycast params object for next raycast
RaycastParams.FilterDescendantsInstances = {secondHitPart}
-and repeat

This thread addresses your question:

2 Likes

You can use RaycastParams:AddToFilter method to add instances to the filter.

According to the docs this is the way to do it.

1 Like

My only concern is that I don’t want the part to stay in the filter afterwards, and the filter is based on collection service etc so it changes through playing.

This doesn’t allow you to remove already added instances though.

Would it also work to spawn the ray inside of the ignored part as a solution?

local Character = script.Parent

local RaycastParams = RaycastParams.new()
RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
RaycastParams.FilterDescendantsInstances = {Character}

task.wait(3)
local Raycast = workspace:Raycast(Character:GetPivot().Position, Vector3.new(0, -5, 0), RaycastParams)
print(Raycast) --raycast info (intercepts baseplate)

task.wait(3)
RaycastParams.FilterDescendantsInstances = {Character, workspace.Baseplate}
Raycast = workspace:Raycast(Character:GetPivot().Position, Vector3.new(0, -5, 0), RaycastParams)
print(Raycast) --nil

task.wait(3)
RaycastParams.FilterDescendantsInstances = {Character}
local Raycast = workspace:Raycast(Character:GetPivot().Position, Vector3.new(0, -5, 0), RaycastParams)
print(Raycast) --raycast info (intercepts baseplate)
1 Like

Yes.

task.wait(1)
local Raycast = workspace:Raycast(workspace.Baseplate.Position, Vector3.new(0.5, 0, 0.5))
print(Raycast) --nil (raycast from inside baseplate doesn't intercept baseplate)
2 Likes