Raycast() doesn't detect water terrain in any way?

I couldn’t get workspace:Raycast() to work properly. It can’t find water terrain in any way.

The relevant code:

local rParam = RaycastParams.new()
rParam.IgnoreWater = false
rParam.FilterType = Enum.RaycastFilterType.Blacklist
rParam.FilterDescendantsInstances = {}

ray = workspace:Raycast(pos, dir, rParam)
if ray.Instance:IsA(“Terrain”) then …

It finds every terrain but water.

I can find water terrain with the same settings if I use FindPartOnRayWithIgnoreList() or (the deprecated) FindPartOnRay()

The relevant code:

local ray = Ray.new(pos,dir)
local instance, position, _, mat = workspace:FindPartOnRayWithIgnoreList(ray, {}, false, false)
if instance:IsA(“Terrain”) then …

What did I wrong with the first script?

With this simple code just to try and recreate your issue, I find none of the issues that you’re talking about.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

function OnClick()
	local Params = RaycastParams.new()
	local MouseRay = Mouse.UnitRay
	local Result = workspace:Raycast(MouseRay.Origin, MouseRay.Direction * 100, Params)
	if Result then
		print(Result.Instance:IsA("Terrain"), Result.Material)
	end
end

Mouse.Button1Down:Connect(OnClick)

I fear it’s your code that’s the problem rather than the :Raycast method itself, specifically the pos and dir vectors.

1 Like

Thank you, your code works properly, it detects water terrain.
I revised my script, and changed it in a way so now it is working properly.

I’m still not sure what was wrong, but your help absolutely helped.