- Home /
Question by
Cspider · Mar 06, 2013 at 09:07 PM ·
terraingenerationgrassdetails
Problem with Terrain.SampleHeight
I am trying to spawn grass on a terrain as it is randomly generated, but the grass doesn't spawn at a consistent height. here is the code:
private const int DETAIL_RESOLUTION = 1024;
private const int DETAIL_PER_PATCH = 8;
private const int size = 500;
// initialization terrain
void Start () {
gameObject.GetComponent<Terrain>().terrainData.SetDetailResolution(DETAIL_RESOLUTION,DETAIL_PER_PATCH);
int[,] newMap = new int[DETAIL_RESOLUTION,DETAIL_RESOLUTION];
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
float height = gameObject.GetComponent<Terrain>().SampleHeight(new Vector3(i + gameObject.transform.position.x, 0, j + gameObject.transform.position.z));
if( height > 11 + transform.position.y){
newMap[i,j] = Random.Range(1, 2);
}
}
}
gameObject.GetComponent<Terrain>().terrainData.SetDetailLayer(0,0,0,newMap);
gameObject.GetComponent<Terrain>().terrainData.SetDetailLayer(0,0,1,newMap);
gameObject.GetComponent<Terrain>().terrainData.SetDetailLayer(0,0,2,newMap);
}
Thanks in advance ;)
Comment
Your answer
Follow this Question
Related Questions
Hide Grass Details 1 Answer
Custom Terrain Details Shader,Custom Detail Shader In 2019.2 2 Answers
How Do I Fix My Grass? 2 Answers