What do you want to achieve?
For my Black Forest game, I’m planning on making a large map with different terrain elevation, and I’d like to know how to use noise to generate something like Trees across the whole map. (Dense)
What is the issue?
The thing is, I don’t know how to do it, and my only concern is how would it generate across different elevations? Would the trees sink into the ground or float?
What solutions have you tried so far?
I’ve tried looking into the dev forum, but all I got was how to generate TERRAIN with perlin noise, but decoration. I’ve searched other places, found nothing.
Additional details
This is the map I’m trying to generate on. It’s larger, but I’d like to make a map with terrain and have it generate the decor itself.
Do you mean the X and Z values being the area in which I’d like to generate in? And,
Could you tell me how to do this? I’ve never done it before (except from a tutorial), so now this makes me curious! Cause for example, I’d like to generate it to look like what it does in the picture:
While the documentation says it simply accepts a Vector3 value in the second parameter, it’s basically asking you for a laser and how long it should be.
So, Vector3.yAxis would be the direction and -1000 would be the length (downwards).
You’ll probably also want to add in a RaycastParams into the third parameter of the :Raycast method so the trees don’t spawn on top of each other.
Thank you. Now, my other questions would be, first, how would I prevent visuals of for example, the provided decoration floating a tiny bit over the terrain?
Secondly, how would I give it a way to choose a random decoration to put down, like: A random ROCK, or a random TREE, a random BUSH, or one of these three: Log, Bush, or Normal Bush?
Third of all, how would I make it so when it places it, it sets a random size (x,y,z being all the same) and prevents “offset” or floating, like mentioned in the first question?
I’d recommend to also use a different perlin noise (the math.noise function but your X or Z value offset by some constant) and then compare it to some value (so if between 0 and 0,.2 should be a tree, etc. This way, trees are grouped together, bushes are grouped together.)
For rocks and stuff, probably just a math.random() would do. So maybe a trailing else statement if a tree or bush wasn’t chosen.
So you’d want to iterate through the X and Z axis across your entire terrain. That’s where you get your position from.
There’s an edge case if your terrain gets too high, which could result in the raycast starting under the terrain and result in no trees spawning.
Yes, exactly what I mean.
And, how would I iterate through the X and Z axis across my entire terrain, so I could get my position, and where would I put it?
local position --[[Y position : 10.031]]
local noise = math.noise()
local raycastResult = game.Workspace:Raycast(position, Vector3.yAxis * -1250)
local hitPos = raycastResult.Position
local normal = raycastResult.Normal
for x = startingX, endX, step do
for z = startingZ, endZ, step do
-- blah blah
local position = Vector3.new(x, YOUR_Y_VALUE_HERE, z)
-- etc, etc.
end
end
As for the tree placement thing, the first thing that pops into my mind is doing additional raycasts slightly offset from the centre (the offset would be the radius of the tree trunk).
But that’s double the work for each tree. Gimme a 'mo to think of something better.
local position
local startingX, endX = -45.093, 82.651
local startingZ, endZ = -23.093, 99.062
local step = 5
for x = startingX, endX, step do
for z = startingZ, endZ, step do
-- blah blah
position = Vector3.new(x, 10.031, z)
-- etc, etc.
end
end
local noise = math.noise()
local raycastResult = game.Workspace:Raycast(position, Vector3.yAxis * -1250)
local hitPos = raycastResult.Position
local normal = raycastResult.Normal
Since you mentioned putting that code to the very bottom of the for loop, I came up with this:
local position
local startingX, endX = -45.093, 82.651
local startingZ, endZ = -23.093, 99.062
local step = 5
for x = startingX, endX, step do
for z = startingZ, endZ, step do
-- Set the position to the current coordinates
-- that have been gathered from startingX to endX and from
-- startingZ to endZ, with a step of 5.
position = Vector3.new(x, 10.031, z)
local noise = math.noise(18.381, 10.031, 37.164)
local raycastResult = game.Workspace:Raycast(position, Vector3.yAxis * -1250)
local hitPos = raycastResult.Position
local normal = raycastResult.Normal
end
end
position = Vector3.new(x, 10.031, z)
local noise = math.noise(18.381, 10.031, 37.164)
local raycastResult = game.Workspace:Raycast(position, Vector3.yAxis * -1250)
local hitPos = raycastResult.Position
local normal = raycastResult.Normal