I have a ray casting weapon in my game, but It doesn't work right. I want the ray to go through parts if they are invisible but can't figure out how...
--script I use for the ray local ObjectHit, LocationHit = workspace:FindPartOnRay(TrajectoryRay, player.Character, false, true) if ObjectHit.Transparency == 1 then --what should I do here??? --1. make a new ray from LocationHit straight forward? --2. Is there some ignore invisible parts command I could do? end
I want this to not hit parts that are invisible, or at least act like it did not. The ray has already been created but I did not think it was important because I already have it working just not for the invisible parts.
The Roblox Raycasting API does not yet support developer hit test filters apart from ignoring water. Most of the time, because [hopefully] your ray is only going to pass through a few transparent parts, the best solution is to cast your ray, and if the hit part has transparency of 1 (or whatever threshold you want), add the part to the ignore list and cast the ray again. Repeat until you hit either something opaque, or nothing at all.
Note that most of the time, it's not a good idea to add a bunch of transparent items to your ignore list up front, because size of the ignore list can impact cost of the raycast considerably. I've seen code where the developer used CollectionService or Workspace:GetDescendants() to build an ignore list of hundreds of parts and passed it to every raycast call. Performance of this can be bad, particularly if these parts themselves have children (because they will be recursively searched for each ray cast).