Raycasts can completely miss the target when close enough

I am aware this should be in bug reports, but I do not have access to it.

When the ray origin is close enough to an object (close, but not clipping into it), the ray can completely miss the object that is in front of it. One instance of this is trying to raycast from a ray hit position.

Reproduction Steps:

  • Go into the new roblox baseplate

  • Make sure you have the spawn point or a part with a size of 12, 1, 12, and a position of 0, 0.5, 0 (this happens with all parts, not just the spawn point)

  • Raycast 2 times, one time to get a hit position, the second time, to raycast from that hit position, with the exact same direction as the previous raycast.

Code:

local rayResult = workspace:Raycast(Vector3.new(0, 2, 0), Vector3.new(0, -120, 0), RaycastParams.new())
local rayHit = rayResult.Position

local rayResult2 = workspace:Raycast(rayHit, Vector3.new(0, -120, 0), RaycastParams.new())

print(rayResult2.Instance == rayResult.Instance) -- // this SHOULD be true

Output: false

Video of it actually being a problem:


So the raycast works but ends up not working when it’s first position is very close to the target position?

Yeah, if you hit a part with a raycast, then do another raycast from that ray hit position, it should hit the same part, but doesn’t.

That’s just how raycasts work, the ray will ignore the part if the ray’s origin is inside of the part. I assume the origin being at the exact position where the previous ray hit the part is close enough for the engine to ignore it.

If you really need to fix this (although I’m not sure why you would, since you already know that there is probably a part right under from the first raycast’s result [unless you’re shooting the first ray upwards or something]), you could try translating the second ray’s origin by a very small distance opposite to the direction.

Something like this:

local direction = Vector3.new(0, -120, 0)
local rayResult2 = workspace:Raycast(rayHit - direction.Unit * 0.0001, direction)

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