I’m trying to get the thickness of a wall from a point, for sound occlusion. Idk if this is efficient or anything but I thought I could raycast in increments (stepValue) throe the wall until the raycast didn’t return. But it doesn’t return anything when the ray starts in the wall.
function GetWallThickness(wall:Part, NormalVector:Vector3, HitPosition:Vector3, stepValue:number, maxSteps:Int)
local rayParamiter = RaycastParams.new()
rayParamiter.FilterDescendantsInstances = {wall}
rayParamiter.FilterType = Enum.RaycastFilterType.Include
for i=1,maxSteps,1 do
local raycastResult = workspace:Raycast(HitPosition+(NormalVector*i), NormalVector*stepValue, rayParamiter)
if raycastResult == nil then
return i*stepValue
end
end
return 1
end
--(This runs in renderSteped so every frame)
local d = GetWallThickness(ray.Instance, direction.Unit, ray.Position, 1, 10,) --distance
Is it just impossible to do it this way, and is there a way more affective way of doing this?
Rays will only hit the rendered surface of a mesh so shooting a ray from the inside out will return nil. The best way I know to find the thickness of a wall is to fire a ray in the opposite direction of the original so you can find the thickness based off the distance of the two rays
The red arrow is the origonal ray, and the green is the backwards one. Its starting postition could be whatever you want it to be, but its direction must be the opposite of the red vectors. The thickness is simply just the distance between the two points.
For more accurate thickness messures, I could come up with a better solution, but for now, I think this would work fine.
One way to detect thickness is to instead detect the opposite surface of the wall. You can do this by doing incremental raycasts facing backwards (with the filter set to whitelist and the instances set to the wall), until one of the incremental raycasts finds an opposite surface. When the incremental raycasts find an opposite surface, the thickness is then the magnitude of the first surface position minus the position of the opposite surface found with the incremental raycasts.
As for efficiency, the cost of raycasting is almost entirely based on the length of the raycast, so doing a lot of short raycasts is basically just as expensive as doing one long raycast.