Raycast misses Terrain in a specific pattern

I was playing around with raycasts, and I wanted to detect the terrain under a bunch of parts.Turns out that for some reason, the raycasts sometimes misses the terrain completely.

I investigated this issue, and it turns out the misses aren’t just random, they occur in a specific pattern - the outlines of square chunks around 128 studs wide (see picture to understand what I mean).
Another thing I find very strange about this is that this only occurs in places that have hills. On flat areas it works fine and never misses.
Below is the script I used to create what you can see on the image. Basically it creates parts on a bunch of different places, each part fires a raycast downwards, if it finds something nothing happens with it, but if it doesn’t find something (misses the terrain), it turns red and gets the neon material. (To be clear, the image is how it looks when the script has finished running.)

local XStart = -200
local XEnd = 200
local ZStart = -200
local ZEnd = 200

local samplePart = Instance.new("Part")
samplePart.Size = Vector3.new(2,2,2)
samplePart.Transparency = 0.7
samplePart.Material = Enum.Material.SmoothPlastic
samplePart.Anchored = true

for xPos = XStart, XEnd, 1 do
	for zPos = ZStart, ZEnd, 1 do
		
		local newPart = samplePart:Clone()
		
		newPart.Position = Vector3.new(xPos*4, 45, zPos*4)

		local terrainCheck = Ray.new(newPart.Position, Vector3.new(0, -100, 0))
		local hit, p, _, m = workspace:FindPartOnRay(terrainCheck)
		
		if not hit then
			newPart.Color = Color3.new(1, 0, 0)
			newPart.Material = Enum.Material.Neon
			newPart.Transparency = 0
		end
		
		newPart.Parent = workspace
	end
	wait()
end

You can see that where there are hills, there are red cubes organized in a pattern, every red cube indicates that it couldn’t detect the terrain below it.
Any idea why this is occuring?

3 Likes

Are you starting the ray inside the mountains? Why don’t you increase the height to more than 45 studs?

1 Like

If that doesnt work have you tried setting terraincellsarecubes to true?
It might fix some weird slope related accuracy issue

workspace:FindPartOnRay(terrainCheck,nil,true)

Along with the previous ideas, also try visualizing the ray as a part to debug it here is a module that could work for that purpose.

1 Like

Thank you for the suggestion! My module is very poor in features and has certain weird problems, there exists a better one that I’d recommend instead:

1 Like

@JarodOfOrbiter

Are you starting the ray inside the mountains? Why don’t you increase the height to more than 45 studs?

I am not, the ray is started from the part’s position which is well over the terrain surface, I have tried putting the parts higher up but no difference.

@PapaBreadd

If that doesnt work have you tried setting terraincellsarecubes to true?

That didn’t help either, same behaviour.

@dthecoolest

also try visualizing the ray as a part to debug

I don’t think that would help me, after all I do know where exactly I’m shooting the ray.

Can replicate this issue. As far as I know some beta feature was causing it and it seems that now the feature has been fully released. Seems that the raycasts don’t work completely on terrain region bounds.


Here’s a screenshot from my minimap rendering tool which shows the issue (changed the background color to red).

2 Likes

We’ve contacted ROBLOX with our accelerator team and they said they are now working on a fix.

4 Likes