Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

TerrainData.bounds Documentation

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

  1. rstepanek

    rstepanek

    Joined:
    Apr 7, 2017
    Posts:
    2
    Hello,

    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?
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,288
    Hi Rsteppanek,

    Look at:

    https://docs.unity3d.com/ScriptReference/Bounds.html

    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

    rstepanek

    Joined:
    Apr 7, 2017
    Posts:
    2
    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.

    t.Trasnform.position;
    if(t.terrainData.bounds.contains(myPointVector)){
    var height = t.SampleHeight(object_pos);
    };


    ---or---
    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){
    2.  
    3. var min = t.transform.position.x - t.terrainData.bounds.extents.x;
    4. if (min < point.x) return false;
    5.  
    6. var max = t.terrainData.bounds.extents.x + t.transform.position.x;
    7. if (max > point.x) return false;
    8.  
    9. var zmin = t.transform.position.z - t.terrainData.bounds.extents.z;
    10. if (zmin < point.z) return false;
    11.  
    12. var zmax = t.terrainData.bounds.extents.z + t.transform.position.z;
    13. if (zmax > point.z) return false;
    14.  
    15. return true;
    16. }
    17.  
    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

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    641
    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.terrainData.bounds.center + terrain.transform.position, terrain.terrainData.bounds.size);