sampling height of terrain when there is more than one terrain in the scene?

I am trying to make an enemy follow the player along the terrain. I know that using Terrain.activeTerrain.SampleHeight(transform.position) will give me the height of the terrain if there is only one in the scene, but if there are more how do I tell it which terrain to pull the height from?

I'm not sure if there is a better way, but you could try raycasting straight down from your "transform.position" you are using.

1 Answer

1

punisher is right, to define the GameObject of the terrain mesh you want to sample the height from, you should raytrace downwards from the player/enemy that you need to measure the height from, you can also use sphereoverlap. You can also get all the terrain positions, from their tag, and compare their distance with the object you want to measure the terrain from, and takes the closest one with the unity distance() function.

this worked pretty good except it is shaky. I think probably because it has to do so much every frame. Any ideas on how to make it more smooth?

I attached a box collider to all the terrain tiles, so when the enemy enters the trigger it will set that terrain to the variable that is used to get the heights. This works pretty smoothly.