Terrain Raycasting Woes

Now, I have a script that when I click on terrain based water, it fires a remote event. If mouse.Target returns with terrain, It uses a mouse.UnitRay to find if the patch that was clicked is water. Problem is, it always returns with Enum.Material.Air. The only exception is sometimes when you jam your camera into the ground, it returns what the ground’s material was. I tried ignoring Enum.Material.Air, but it only takes Instances. (I knew that, but it was worth a try.) I am completely out of ideas.

Here is my code: (Raycasting system at 20 - 26, this is all in a local script)

player = game.Players.LocalPlayer
character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end
mouse = player:GetMouse()
event = game.ReplicatedStorage.HarvestEvent

mouse.Button1Down:Connect(function()
	local target = mouse.Target
	if character:FindFirstChildOfClass("Tool") == nil then
		if target then
			if target.Name == "Trunk" and player:DistanceFromCharacter(target.Position) <= 15 then
				if math.random(1,10) == 10 then
					event:FireServer("Stick")
				end
				target.Parent.Leaves.ParticleEmitter.Enabled = true
				wait(1)
				target.Parent.Leaves.ParticleEmitter.Enabled = false
			elseif target.Name == "Terrain" then
				local _, _, _, material = workspace:FindPartOnRay(mouse.UnitRay)
				print(material, type(material))
				if material == Enum.Material.Water then
					player.Thirst.Value = player.Thirst.Value + 25
				end
			end
		end
	end
end)

The issue here is that Mouse.UnitRay is actually a really short ray with a length of 1 (that’s what unit vectors are). In most cases, except when you jam the camera into the ground, one stud in front of the camera is air.

So to fix this you need to increase the length of the ray. You can do this by multiplying it by a big number (i.e. Mouse.UnitRay * 1000) to increase the range.

Just tried it, and I’m getting an error for multiplying a userdata value by a number. Is there a writable range property or something I’m missing? Sorry if I’m being dumb, never used rays before.

Whoops, my bad, you can’t multiply Rays. You’ll need to make a new ray using the origin and direction (which is what you actually multiply) of the UnitRay:

local UnitRay = Mouse.UnitRay
local NewRay = Ray.new(UnitRay.Origin, UnitRay.Direction * 1000)

Alright, it worked! Thanks a bundle!