Raycast doesn't return anything if it starts inside a part [HELP]

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.

amazing illustration of what I’m trying to do:

function for getting wall thickness:

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?

Yes, there is a better way to do this. It has already been documented. I’m not tryna be rude. Look up on Google, end it with DevForum.Roblox.com.

EXAMPLE: “How to Play Animations? DevForum.Roblox.com

Staff don’t want a bunch of posts that are almost exactly similar to already posted solutions. It gunks up the platform and makes things hard to find.

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

hope this helps :grin:

2 Likes

This is close, but not quite right:

Raycasts detect surfaces, not volumes.

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.

Screenshot 2024-09-05 at 5.57.31 PM

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.

2 Likes

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