How to make a Ray go though invisible/cancollide objects?

Hello! I’m working at my gun, however my game has lots of invisible and cancollide objects, and the ray detects the invisible objects and not the zombies sometimes.

I have a ideea, however I’m not sure how to make it. The ideea was to continue the ray from the point where it hit the invisible part, in the direction where the bullet should go, and repeat if there are more invisible parts. I don’t need any code for the Gun, only for the ray

Thanks,

  • Bloxxy
1 Like

Create two collision groups.

  1. Collision group for cancollide off and invisible objects, which do not collide with the raycasting group
  2. Collision group for the ray
1 Like

There are a couple of ways. The way you mentioned is one of them. Alternatively, you can actually set the collision group of the Raycast to be different from the in invis parts by doing

local RaycastParameters = RaycastParams.new()
RaycastParameters.CollisipnGroup = "Zombie"

Or you can set the Raycast to only collide with the objects that you specifies or if ore object you do not want it to hit.

RaycastParameters.FilterType = Enum.CollisionFilterType.Whitelist
RaycastParameters.FilterDescendantInstances = {}-- Table of instances of Zombies

RaycastParams | Roblox Creator Documentation for more info.

3 Likes

Just found an fix.

So, I made 2 functions

One to simply raycast

And the other one keeps raycasting. If the hit is nothing, return the result. If the hit is a invisible/cancollide off part, then add it to the Ignore table and continue. If the result is a normal part, return the result.

Pretty simple!

2 Likes

Great that you found a solution, but I’d recommend you to use raycast collision groupings instead. While the performance difference is minimal, using collision filtering would still require less script activity anyway.

2 Likes