Problem with random object generation using raycasting

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want my code to work correctly, place the objects randomly across the map’s terrain.

  2. What is the issue? The objects get spawned in the air all together, 100 of them.

  3. What solutions have you tried so far? None lmao.

local ObjectsFolder = game.ServerStorage:WaitForChild("Objects")

for i = 1, 100, 1 do
	print('Test 1')
	for _, folder in (ObjectsFolder:GetChildren()) do
		print('Test 2')
		for _, object in (folder:GetChildren()) do
			print('Test 3')
			local RandomXValue = math.random(-1192.595, 1220.259)
			local RandomYValue = math.random(-1190.396, 1263.925)
			local Origin = Vector3.new(RandomXValue, 32.715, RandomYValue)
			local RayDirection = Vector3.new(0,-500, 0)
			local params = RaycastParams.new()
			params.IgnoreWater = true
			params.FilterType = Enum.RaycastFilterType.Whitelist
			params.FilterDescendantsInstances = {workspace.Terrain}
			local raycastResult = workspace:Raycast(Origin, RayDirection, params)
			print('Test 4')
			if raycastResult then
				print('Test 5')
				if raycastResult.Instance:IsA("Terrain") then
					print('Test 6')
					local ObjectClone = object:Clone()
					ObjectClone.Parent = workspace
					ObjectClone.Anchored = true
					ObjectClone.Position = raycastResult.Normal
				end
			end
		end
	end
end

Normal is actually a representation of direction, not a position. And it will be really close to (0,1,0), hence why all the trees are spawning in the same spot. You probably want ObjectClone.Position = raycastResult.Position

Alright, I’ll try and then let you know how it goes.

Thanks, it worked! One last thing though, the trees and stuff get spawned basically inside the terrain, even for water. How will I be able to fix this?
image

I tried doing ObjectClone.Position = raycastResult.Position + Vector3.new(0, ObjectClone.Size.Y, 0) but that doesn’t really work, this just happens:



image

1 Like

For the halfway in the ground bit, I think it’s due to the size of the tree. I can see from the first photo you posted that all of the trees are being positioned from the center. So just move them up by half of their height.

As far as the water goes,

make that false and add this check instead:

-- put this at the print('Test 6') location
if raycastResult.Material == Enum.Material.Water then continue end

I removed the params.IgnoreWater = true and none of the objects were spawned in the map. But some of the trees were still spawned in the water, I did if raycastResult.Material == Enum.Material.Water then return end

Here’s the updated script:

local ObjectsFolder = game.ServerStorage:WaitForChild("Objects")
local CollectionService = game:GetService("CollectionService")

for i = 1, 100, 1 do
	print('Test 1')
	for _, folder in (ObjectsFolder:GetChildren()) do
		print('Test 2')
		for _, object in (folder:GetChildren()) do
			CollectionService:AddTag(object, "Objects")
			print('Test 3')
			local RandomXValue = math.random(-1192.595, 1220.259)
			local RandomYValue = math.random(-1190.396, 1263.925)
			local Origin = Vector3.new(RandomXValue, 32.715, RandomYValue)
			local RayDirection = Vector3.new(0,-500, 0)
			local params = RaycastParams.new()
			params.IgnoreWater = true
			params.FilterType = Enum.RaycastFilterType.Whitelist
			params.FilterDescendantsInstances = {workspace.Terrain}
			local raycastResult = workspace:Raycast(Origin, RayDirection, params)
			print('Test 4')
			if raycastResult then
				print('Test 5')
				if raycastResult.Instance:IsA("Terrain") then
					if raycastResult.Material == Enum.Material.Water then return end
					local ObjectClone = object:Clone()
					ObjectClone.Parent = workspace
					ObjectClone.Anchored = true
					ObjectClone.Position = raycastResult.Position + Vector3.new(0, ObjectClone.Size.Y/2, 0)
				end
			end
		end
	end
end

Use continue, not return, and then try again with IgnoreWater set to false.

if raycastResult.Material == Enum.Material.Water then
    continue
end
1 Like

It worked, some of the small trees are still somewhat inside the terrain but I guess it can’t be fixable, thanks for helping me!

2 Likes

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