Growing / Seeding Trees In Certain Regions Of Terrain

Hello! I am currently working on a tree generation script. What I’m trying to do is allow trees to grow on certain “regions” of terrain. The best option I’ve came up with is putting a part in an “region” of terrain and the trees will grow in that “part region” but they’ll grow perfectly on the surface of terrain.

The trees I set in the script will grow in that part region on the surface of the terrain inside of the part region

What’s the best way of approaching this?

I think you can use random to do it like this

1 Like

It sorta helps with my issue / idea but it still really doesn’t give me much info on getting the surface and such.

You could use Raycasting to find the distance between the terrain and the center of the tree hitbox (or the PrimaryPart if you want) and add or subtract an offset to better fit the ground.

You could do something like this if you can ensure the trees are above the surface:

local RayResult = workspace:Raycast(PrimaryPart.Pos,Vector3.New(0,-100,0),RayParams)

RayResult returns something called a RaycastResult (API ref) that has a property called Position, or the Vector3 of where the ray and ground intersected. From there you could do something like this:

local Intersect = RayResult.Position --A Vector3
--Putting the PrimaryPart directly at the intersect makes half of it above ground
--Therefore, we add the other half to the final total
local FinalY = (PrimaryPart.Size.Y/2)+Intersect.Y --You can add an offset too

Edit: Make sure to use a RaycastParams (API ref) To filter out the tree model, or else the raycast will just intersect the tree itself and that’ll achieve nothing

1 Like

Sorry for just getting back with you on this. I really appreciate what you’ve taught me here; although I do have a concern. This does teach me how to move the tree model to the surface, but I’m needing the ability to detect a certain position for a tree to grow before it’s grown (the seed pretty much). I’d like to know how to get a random position on the surface of the terrain in that region. I think the technical term would be I’m trying to raycast inside the region part, when the ray detects terrain, it’ll detect the surface of the terrain and return a random point on the surface of the terrain.