Need help with raycasting math

So I’m working on a penetration system for my bullet physics but I’m running into an issue that doesn’t make sense.

I need to find the thickness of an object the raycast hits. I do this by trying to cast another ray coming from the opposite direction. From there I would find the thickness by just getting the distance between the ray’s hit positions through subtraction. I haven’t gotten to that part yet though because THE SECOND RAYCAST IS NIL AND IDK WHY.

The second ray is not hitting anything despite it facing the direction of the instance I need it to hit.

Here is a visualization of what I’m trying to do (I included variable names in quotes):

And this is the code that handles what I’m trying to do:

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {result.Instance}
	
local latestTrajectory = cast.StateInfo.Trajectories[#cast.StateInfo.Trajectories] -- this is just how I get the origin of the original raycast ("result")
local direction = -(result.Position - latestTrajectory.Origin).Unit
local origin = result.Position - (direction * 50)
--debugPart(origin, "ThicknessRayOrigin")
--debugPart(origin + direction * 10)
	
local thicknessRay = workspace:Raycast(origin, direction, params)
print(thicknessRay)

maybe I’m overlooking something simple and being dumb but either way thanks in advance.

1 Like

nvm I figured out what I was doing wrong.
when raycasting i forgot to multiply direction by a distance :sob:

new code:

local thicknessRay = workspace:Raycast(origin, direction * 50, params)
1 Like

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