How to use terraindata.GetHeight(x,y) [duplicate]


How to use terraindata.GetHeight(x,y) [duplicate]



This question already has an answer here:



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.



enter image description here



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;
}
}



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





1. You are suppose to pass 0 to the y axis of the SampleHeight. Right now, you are passing the z value to it. 2.You also nee to apply some offset. The duplicate has a complicate example on how to instantiate object within terrain area.
– Programmer
Jul 3 at 7:30


0


y


SampleHeight




1 Answer
1



Have you tried using Terrain.SampleHeight() ?

Popular posts from this blog

PHP contact form sending but not receiving emails

PHP parse/syntax errors; and how to solve them?

iOS Top Alignment constraint based on screen (superview) height