Raycasting against multiple collision groups

What is the most efficient way to to raycast against multiple collision groups? For example, imagine I have ForceWalls (transparent) and BrickWalls (Opaque) , but I would like to see if I have line of sight to a Default object in the distance. I want the ray to be blocked (hit) by BrickWalls, go through ForceWalls and confirm a hit against the Default object. I don’t want to change my actual collision matrix as that will cause other problems (for example I do not want default to be able to walk through any type of wall).

I was hoping that RaycastParams CollisionGroup parameter would accept a table or comma separated string, but it does not appear to.

Thoughts welcome, thanks.

I see. Collision groups and CanQuery are out of the question. The simplest solution is the raycast filter. All transparent walls in a folder and this folder added to the filter.

I’m sure this is perfectly fine for most games as long as parts constructing the entirety of these walls are not in extreme numbers (talking about thousands). The filtered instances are flattened into an array and evaluated before casting, and the fact is that the operation grows more expensive with each additional instance, but since the walls are invisible they can most likely cover relatively large areas with few parts.

Only if your folder has tons of these parts to ignore should you look for alternative solutions (like perhaps only filtering instances in the reachable surroundings around the player - just brainstorming here).

2 Likes

Hey, thanks for the thoughts. It seems filtering is the way to go for now.

1 Like

My pleasure!

Last time I was working on something similar I also just added the transparent walls to the filter list. As you determined, I’d first finish the game too and inspect for performance bottlenecks afterwards. Chances are the filter is perfectly alright, but if you really sense performance issues, you could invest your time into more complicated optimisations. There’s the mentioned filtering in a radius, running code in parallel, and probably more.

I bet you’d need thousands of invisible parts to cause lag though.

Good luck :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.