Hello everyone im trying to make a script that will spawn plants on a terrain at the x, y and z position specified using the terrainData.GetHeight() function. The X and Z coords work fine but for the y position if only ever gives me the default/flat height that the terrain started at even when the terrain height is bigger than the default/flat height. So basically everything works fine when im spawning the plants on a flat part of the terrain but any hills will not change the heights they spawn at.
picture above the part of the terrain that has no plants does have them. they are just under it on the same level as the others.
see code below. thanks in advance for trying to help.
(:
private void spawnPlants() {
for (int x = 0; x < amountOfPlants; x++) {
float randomX = Random.Range(transform.position.x, transform.position.x + GetComponent<Terrain>().terrainData.size.x);
float randomZ = Random.Range(transform.position.z, transform.position.z + GetComponent<Terrain>().terrainData.size.z);
GameObject newPlant = Instantiate(plants[Random.Range(0, plants.Length)], new Vector3(randomX, GetComponent<Terrain>().terrainData.GetHeight((int)randomX, (int)randomZ), randomZ), Quaternion.identity);
newPlant.transform.parent = parent.transform;
}
}
Have you tried using Terrain.SampleHeight() ?