Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make rays ignore invisible parts?

Asked by
OBenjOne 190
6 years ago

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.

0
^Yes, add transparent parts to an ignorelist Vulkarin 581 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

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).

0
Thanks! this worked great! OBenjOne 190 — 6y
Ad

Answer this question