How would I go about setting a length limit for my raycast?

As I wish for the raycast to only go a certain distance, as I’m creating a mining system and making sure that the player is within reasonable distance of the ore is what I wish to do. I’m rather new to using the raycast system, thus any help would be appreciated! Below you’ll find the code I already have in terms of raycasting

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

Pickaxe.Activated:Connect(function()
	Hitbox.Touched:Connect(function() end)
	local function Check()
		for _, part in pairs(Hitbox:GetTouchingParts()) do
			Count += 1
			local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2, params)

Ray casts only go the length of the direction parameter, they don’t go on forever. So to have a length limit you’d just set the coefficient on the second parameter to how many studs forward you want to ray cast. Right now it’d be two studs.

------------------------- Ray only goes 4 studs forward
workspace:Raycast(Pos, Dir * 4, params)
2 Likes