Is it possible for a raycast to ignore terrain?

As I have a system where ore appears in specific locations within a cave, however, the terrain seems to be blocking the hitbox from being touched. Would there be a way to make it so that the raycast only gets obstructed by parts and not terrain, as I believe the terrain is obstructing the ore models from being touched. Below you’ll find the script responsible for this system.

local IronOre = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Hitbox = script.Parent:WaitForChild("Hitbox")
local replicatedStorage = game:GetService("ReplicatedStorage")
local OreTouchedEvent = replicatedStorage.OreTouchedEvent
local Character = Player.Character or Player.CharacterAdded:Wait()
local Pickaxe = Character:WaitForChild("Pickaxe")
local Handle = Pickaxe:WaitForChild("Handle")
local Count = 0
local Active = false

Hitbox.Touched:Connect(function(player)
	local function Check()
		if Handle:GetAttribute("IsActivated") == true then
			Count += 1
			local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2)
			if Cast then
				if Cast.Instance.Name ~= IronOre.Name then return end
			else
				return
			end
		end
	end

	repeat task.wait(1) Check() until Count == 5
	OreTouchedEvent:FireServer(IronOre)
	Count = 0
end)

Raycasts can have Params, which can allow you to exclude objects from the list of things it can hit, maybe try putting terrain in the list?

2 Likes

This works.

You could also use the CollisionGroup in a RaycastParams to exclude terrain

2 Likes

The terrain is just a BasePart, like MeshParts and Parts, so you can ignore it in the same way using a raycast params:

local Params = RaycastParams.new()
-- Set the raycast to ignore the items in the filter
Params.FilterType = Enum.RaycastFilterType.Exclude
-- Set the list of BaseParts to ignore
Params.FilterDescendantsInstances = {workspace:FindFirstChildOfClass("Terrain")}
-- Do your raycast (with the `Params` made above)
local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2, Params)
1 Like

Hmm… I tried changing the raycast params, however, it seemingly isn’t affecting the ore… I’m wondering if I’ve done it right or not.

local params = RaycastParams.new()
params.FilterDescendantsInstances = {workspace:FindFirstChildOfClass("Terrain")}
params.FilterType = Enum.RaycastFilterType.Exclude

Hitbox.Touched:Connect(function(player)
	local function Check()
		if Handle:GetAttribute("IsActivated") == true then
			Count += 1
			local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2, params)

That’s definitely correct for ignoring the terrain.

Are you sure it’s the terrain getting in the way? It might also be the character or other parts of the tool. You could do:

Hitbox.Touched:Connect(function(player)
	local function Check()
		if Handle:GetAttribute("IsActivated") == true then
			Count += 1
			local params = RaycastParams.new()
			-- Added the character to the filter, which also ignores the tool
			params.FilterDescendantsInstances = {workspace:FindFirstChildOfClass("Terrain"), Character}
			params.FilterType = Enum.RaycastFilterType.Exclude
			local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2, params)

I’m now facing a new issue, however, it isn’t because of the code I had received from you all! Thanks for helping!

1 Like

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