Fast method for getting Terrain height values

Questions & AnswersCategory: ProgramingFast method for getting Terrain height values
hurtfultoad asked 4 years ago

I’ve got custom item and AI placement code but I need to be able to obtain top height values (biome / voxel type would be nice as well) at scene initialization so that I can land these objects directly on topmost Y when they’re spawned. This needs to happen before the colliders may have been generated so I don’t want to use a Raycast. What is the best way to obtain these values?
BTW: Congratulations and thanks for this iteration of uTerrains. I’ve got a lot of development time and money invested in other solutions (TerrainEngine, Voxelfarm, Voxeland, etc) but with the recent update, you’ve got me moving everything into a new uTerrains project because it offers substantial advantages over the rest. 

2 Answers
Virality answered 4 years ago

I used terrain.GetVoxelAt in one of my codes to get the height of the voxels at this position. Not sur if it’s useful or not

hurtfultoad answered 4 years ago

I’ll answer my own noob question… A public terrain Raycast is in UltimateTerrain.cs that uses GridUtils to get heights regardless of collider generation.
ex: This reports height at given X,Z
 
void getHeight(float posx, float posz){
Vector3 myhit, mynorm;
Ray myTRay = new Ray();
myTRay.direction = Vector3.down;
myTRay.origin = new Vector3(posx, 5000f, posz);
Terrain.Raycast(myTRay, 7000f, out myhit, out mynorm);
Debug.Log(“Hit at: “+myhit);
}

hurtfultoad replied 4 years ago

OK, and that only worked for areas where the chunk had been generated. I finally got what i needed —-> a true world-wide getHeight(x,z) function by collecting all used 2D generator values with Compute2DValues(double x, double z, double[] out_Values2D) function and adding their values together.