So I have a 3d terrain and some daikons, I want these daikons to be distributed around a terrain, they all need to be surface level so the player can collect them, how would I do this?
Use divide and conquer to split the problem into smaller, easier to solve problems.
- Distributing the items over some area
- Placing the items at the correct height for your terrain
For distribution, you can keep it arbitrarily simple. You could just calculate random coordinated in the range of 0-maxX and 0-maxZ of your map and use a downwards raycast to check if this location is actually over terrain. You can do this until you found enough valid XZ locations for your object count to spawn.
Or you could rely on some algorithms like poisson disc sampling which effectively do the same random distribution, but also make sure to not spawn stuff too close to each other, based on your distance criterias. For more complex terrain shapes, you should still do the downwards raycast to check if you actually hit terrain, as poisson disc sampling is only about distributing points in some shape (usually a square).
To place the items you now only need the correct Y coordinate, since you already have the XZ coordinate. To find that you can use the downwards raycast hit Y value (or better, store it from the previous step), and then simply place the item at this XYZ position. In practice you might want to add half the height of the item to the Y part of its spawn location, and likely also want to adjust the transform.up of the object to the hit.normal (the upwards vector of the hit mesh triangle).
If your terrain can have caves this gets a bit more complex, but you said surface level so i assume that doesnt matter. The above also works if you already have other obstacles (rocks, trees, …) placed on the terrain, however the items will then obviously spawn on top of those obstacles. If this is not desirable, simply use the downwards raycast to check what it hits, and it it’s not the actual terrain, dont accept the position.
Exactly what Yoreki says… and I have some spawning examples in my MakeGeo package too.
Get the project and search for “spawn” in the files.
MakeGeo is presently hosted at these locations:
Thanks both of you! I will try this out