How do I filter out a collision group using Raycast Params?

How do I filter out a collision group using Raycast Params?

local CastParams = RaycastParams.new()
CastParams.IgnoreWater = true
CastParams.FilterType = Enum.RaycastFilterType.Exclude
CastParams.FilterDescendantsInstances = {}```
1 Like

You can specify the Collision Group to use like this:
CastParams.CollisionGroup = "CollisionGroupNameHere"

Any parts that do not collide with that group will be ignored.

Say you have a Collision group called “Test”, if you then make sure Test doesn’t collide with Default, and then set the CollisionGroup in the Cast Params to Default, the parts in Test will be ignored.

For your case, filtering out a single collision group would be as easy as:

CastParams.CollisionGroup = "CollisionGroupName

Unfortunately if you’re trying to do anything more advanced than that, e.g. filter parts from more than one Collision Group, you’d have to use the solution below.

local filter = {}
for _,v in workspace:GetDescendants() do
    if v:IsA("BasePart") and v.CollisionGroup == "CollisionGroupName" then
        table.insert(filter, v)
    end
end
CastParams.FilterDescendantsInstances = filter

This is pretty inefficient but is the only thing you can really do for filtering out different collision groups in the same RaycastParams.

1 Like

But I need test to collide with default. I want the raycast to go straight through test and hit default behind it.

That’s fine, for that you can create a secondary Collision Group which doesn’t collide with Test for the sole purpose of doing the raycasting, and specify that as the Collision Group in CastParams.

Otherwise, you can do as @LegendOJ1 said, though that might be inefficient if you have a lot of parts.

1 Like