- Home /
Randomly spawn an object on a procedurally generated plane
I wanna know how to spawn different objects on my procedurally generated map, like trees or rocks. Here's to code I'm using.
using ProceduralToolkit.Samples.UI;
using UnityEngine;
namespace ProceduralToolkit.Samples
{
public class LowPolyTerrainExample : ConfiguratorBase
{
public MeshFilter terrainMeshFilter;
public MeshCollider terrainMeshCollider;
public RectTransform leftPanel;
public bool constantSeed = false;
public LowPolyTerrainGenerator.Config config = new LowPolyTerrainGenerator.Config();
private const int minYSize = 1;
private const int maxYSize = 10;
private const float minCellSize = 0.3f;
private const float maxCellSize = 1;
private const int minNoiseFrequency = 1;
private const int maxNoiseFrequency = 8;
private Mesh terrainMesh;
private void Awake()
{
Generate();
SetupSkyboxAndPalette();
InstantiateControl<SliderControl>(leftPanel)
.Initialize("Terrain height", minYSize, maxYSize, (int) config.terrainSize.y, value =>
{
config.terrainSize.y = value;
Generate();
});
InstantiateControl<SliderControl>(leftPanel)
.Initialize("Cell size", minCellSize, maxCellSize, config.cellSize, value =>
{
config.cellSize = value;
Generate();
});
InstantiateControl<SliderControl>(leftPanel)
.Initialize("Noise frequency", minNoiseFrequency, maxNoiseFrequency, (int) config.noiseFrequency, value =>
{
config.noiseFrequency = value;
Generate();
});
InstantiateControl<ButtonControl>(leftPanel).Initialize("Generate", () => Generate());
}
private void Update()
{
UpdateSkybox();
}
public void Generate(bool randomizeConfig = true)
{
if (constantSeed)
{
Random.InitState(0);
}
if (randomizeConfig)
{
GeneratePalette();
config.gradient = ColorE.Gradient(from: GetMainColorHSV(), to: GetSecondaryColorHSV());
}
var draft = LowPolyTerrainGenerator.TerrainDraft(config);
draft.Move(Vector3.left*config.terrainSize.x/2 + Vector3.back*config.terrainSize.z/2);
AssignDraftToMeshFilter(draft, terrainMeshFilter, ref terrainMesh);
terrainMeshCollider.sharedMesh = terrainMesh;
}
}
}
can you please add more context? for a question like this please write at least 5 sentences about where you want to place what, what you have tried and why it does not work.
just get the bounds of your plane and generate a random coordinate that is within those bounds. Then instantiate your enemy and set position to the coordinates you generated.
That would work if I want it to fall from space, I want the objects to be spawned on top of the ground with different positions, so I can have a tree without a rigid body be spawned on the ground proceduraly, and I don't know how to achieve this
well one cheap but reliable way would be to raycast on your terrain from a point where you can be sure that it is above your maximum possible terrain height. Then place the object on the hitpoint.
How would you do that, I have no clue how to use raycasts.
Your answer
![](https://koobas.hobune.stream/wayback/20220613044457im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I wanna know how to randomize perlin noise 2 Answers
Scene generation question 0 Answers
Overlapping instantiation issues. Help? 1 Answer
Checking if object intersects? 1 Answer