Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

TerrainData.bounds Documentation

Discussion in 'Documentation' started by rstepanek, Apr 7, 2017.

  1. rstepanek


    Apr 7, 2017

    I am trying to get the bounding box of a terrain so that I can check to see if Vector3 is inside of it and get the height of the terrain at that point. I found the TerrainData.bounds documentation and it has this to say:

    What does that mean?

    Creative use of the English language aside, is there an easy way to check if point is inside a terrain object?

    Better yet, is there a way to force the check into two dimensions so that I can then call getTerrainHeight to get the height of the terrain at that position?
    Novack likes this.
  2. Graham-Dunnett


    Unity Technologies

    Jun 2, 2009
    Hi Rsteppanek,

    Look at:

    which helps. The Bounds for you will make your terrain covered completely by these 6 faces. This is done in local space. The TerrainData.bounds just implements this, and uses Transform.position to let you know more about your terrain in world space.

    Creative use? Yes, use Transform.position to move the terrain into world space. You can now check whether the point (assume this is a world space point) is inside the box surrounding the terrain. The problem here is that this test does not return what you want... just whether the point is inside the box. So, checking the position of the point will need you to grab triangles and determine which side the point is. That I can't answer - maybe you have vertices that give a central position for particular parts of the terrain. Then you can work out how far away the point is from this relevant position. (I'm inventing this...)

    Terrain.activeTerrain.SampleHeight() ???

    Giving you some answers - hope these help.
  3. rstepanek


    Apr 7, 2017
    Thank you for the reply. I'm not sure what the API call for that looks like. If I have a terrain object "t" are you saying I need to call transform.position before checking its bounds? i.e.

    var height = t.SampleHeight(object_pos);

    Is it that I need to get the bounding box extents and add them to the position vector to get the actual bounds of the terrain object in the x and z plane? i.e.

    Code (CSharp):
    1. public bool checkPointInclusion(Terrain t, Vector3 point){
    3. var min = t.transform.position.x - t.terrainData.bounds.extents.x;
    4. if (min < point.x) return false;
    6. var max = t.terrainData.bounds.extents.x + t.transform.position.x;
    7. if (max > point.x) return false;
    9. var zmin = t.transform.position.z - t.terrainData.bounds.extents.z;
    10. if (zmin < point.z) return false;
    12. var zmax = t.terrainData.bounds.extents.z + t.transform.position.z;
    13. if (zmax > point.z) return false;
    15. return true;
    16. }
    18. if(checkPointInclusion(t,object_pos))
    19. {
    20.     object_pos.y = t.SampleHeight(object_pos);
    21. }

    The latter seems really verbose and I would think that the API would already provide a way to do this more succinctly.
    Last edited: Apr 10, 2017
  4. gilley033


    Jul 10, 2012
    A bit of a necro, but I figure others might come upon this thread to find the same answer you were looking for.

    You need to add the terrain position to the terrain data bounds, like so:

    Code (CSharp):
    1. Bounds worldBounds = new Bounds( + terrain.transform.position, terrain.terrainData.bounds.size);