Ray casting not working!

Hi,
i want to achieve that i itterate over all terrain triangles and randomly spawn som vegitation on top of it like grass patches and flowers and stuff.

i can’t just get the parts position and lookvector and move it a bit up since the traingles will be rotated in different ways so i though casting rays top down view and plant them when the ray hit a surface,
https://i.gyazo.com/f2bfd7fb8bf74064cb5617628ce4215f.mp4
problem is it just doesnt work and i have no clue why i tried moving coordinates, changing blacklist/whitelist it just doesn’t wanna work properly.

background
wedges are in game.workspace[“Terrain2.0”] (descendants)

-- raycasting vegetation Test
function DebugPart(position,size,color,shape)
	local p = Instance.new("Part")
	p.Anchored = true
	p.CanCollide = false
	p.Size = size
	p.Shape = shape
	p.Color = color
	p.Parent = game.Workspace.Ignore
	p.CFrame = CFrame.new(position)
	p.Transparency = 0.8
end

function rayCast(CasterBlock)
	local Origin =CasterBlock.Position + Vector3.new(0,30,0)	
	local Destination =  CasterBlock.Position - Vector3.new(0,30,0)	
	-- Build a "RaycastParams" object
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {game.Workspace.Ignore,game.Workspace.Grass}
	raycastParams.IgnoreWater = true

	-- Cast the ray
	local raycastResult = workspace:Raycast(Origin, Destination, raycastParams)
	local randomColor = Color3.fromRGB(math.random(0,255),math.random(0,255),math.random(0,255))
	local Color = randomColor
	DebugPart(Origin,Vector3.new(4,4,4),randomColor,"Cylinder")
	
	
	DebugPart(Destination,Vector3.new(4,4,4),randomColor,"Ball")
	-- Interpret the result
	local distance = (Origin - Destination).Magnitude
	local p = Instance.new("Part")
	p.Anchored = true
	p.Transparency = 0.5
	p.CanCollide = false
	p.Size = Vector3.new(1, 1, distance)
	p.CFrame = CFrame.new(Origin)
	p.Color = Color
	CasterBlock.Color = Color
	p.CFrame = CFrame.lookAt(Origin, Destination)*CFrame.new(0, 0, -distance/2)
	p.Parent = game.Workspace.Ignore
	
	if raycastResult then
		
		local HitBlock = raycastResult.Instance:GetFullName()
		--if HitBlock.Color3 == Color3.fromRGB(20, 80, 20) then
		distance = (Origin - raycastResult.Position).Magnitude
		
			
		local TempCloneGrass = Grass[math.random(1,#Grass)]:Clone()
		TempCloneGrass.Parent = game.Workspace.Grass
		TempCloneGrass.CFrame = CFrame.new(raycastResult.Position + Vector3.new(0,(TempCloneGrass.Size.Y/2)-0.3,0))
		success += 1
		--end
		--print("Object/terrain hit:", raycastResult.Instance:GetFullName())
		--print("Hit position:", raycastResult.Position)
		--print("Hit")
		--print("Surface normal at the point of intersection:", raycastResult.Normal)
		--print("Material hit:", raycastResult.Material.Name)
	else
		--print("Nothing was hit!")
		fail += 1
	end
	
	
	
	
end






for i,v in pairs(game.workspace["Terrain2.0"]:GetDescendants()) do
	if v:IsA("WedgePart") then
		if v.Color == Color3.fromRGB(20, 80, 20) then
			--v.Color = Color3.fromRGB(255,0,255)
			--v.Material = Enum.Material.Grass
			--local TempCloneGrass = Grass[math.random(1,#Grass)]:Clone()
			--TempCloneGrass.Parent = v
			--TempCloneGrass.CFrame = CFrame.new(v.Position) + ((v.CFrame.upVector * ((TempCloneGrass.Size.Y-v.Size.Y))))
			rayCast(v)
		else
			v:destroy()
		end
	end
end

print("Success hits",success)
print("Fail hits",fail)
1 Like

You could try making the terrain a little thicker

i can’t cause it will cause more visual seams
, but is this the reason
testing it out

i tried making it 1 thick same result doesn’t work thanks for the pointer tho!

I believe the problem is your parameters. The second parameter is the direction, not the destination. Your direction should probably be something like Vector3.new(0, -30, 0).

Note:

  • direction = destination - origin
  • origin + direction = destination

Let me know if you’ve got any questions :+1:

4 Likes

TY so much it works finally, wasted hours on somthing thta should be easy

1 Like