How do i create random generation with raycasting

The reason why i want to create random generation with raycasting is because with raycasting you can make it so parts don’t clip into other parts.

I’ve tried looking it up on google and tried scripthelpers but couldn’t get any help and also I’m fairly new to raycasting.

Can you explain what you mean by “random generation”?

sorry, randomly generating trees.

Why would you use raycasting here? You can clone a Model and position its PrimaryPart using Vector3.new(math.random(minX, maxX), 0, math.random(minZ, maxZ)).

would that resolve the issue of parts clipping into each other?

Whoops! No, it most definitely would not. If you want to avoid that, you can use Region3 to check which parts are in a certain region. Look at this Wiki article on using Region3 to check for parts within an area. You could create a Region3 that would look at a random location and place a tree there. If there already is a part, try again until that region is empty.

1 Like

Find the x, y, and z bounds of the map and do some raycasts from above the floor.

Something like this:

local MIN_X = -1000
local MAX_X = 1234
local MIN_Z = -3245
local MAX_Z = 2424
local NUM_TREES_TO_GENERATE = 245
local treeCollection = workspace.Trees

for i = 1, NUM_TREES_TO_GENERATE do
    local xPos = math.random(MIN_X, MAX_X)
    local zPos = math.random(MIN_Z, MAX_Z)
    local ray = Ray.new(Vector3.new(xPos, some_y_position, zPos), Vector3.new(0, -1, 0)*5000)
    local _, hitPos, normal = workspace:FindPartOnRay(ray, workspace.Trees)
    -- clone a tree model, position the tree to hitPos, and parent it to workspace.Trees
end
2 Likes

I’ll most certainly look into this, and get a basic understanding so i can work with it more.

Thanks man, It worked out just fine all i had to do was optimize it a little.

2 Likes