Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TerrainData.GetInterpolatedHeight

Discussion in 'Scripting' started by Ryuuguu, Dec 25, 2007.

  1. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    I see this function used in island demo
    TerrainData.GetInterpolatedHeight
    but it is not listed in my local documentation. It is in the online docs but not on the TerrainData all members page, same goes for GetSteepness . Is this just Docmentation linking error and these function are OK to use and expect to stay in future versions?

    Cheers,
    Grant
     
  2. czx_main

    czx_main

    Joined:
    Feb 18, 2009
    Posts:
    172
    Where is this? :?: :?: :?:
     
  3. Sisso

    Sisso

    Joined:
    Sep 29, 2009
    Posts:
    196
    I have the same doubt and found this post

    The method is used by Island Demo in Heron.js

    Code (csharp):
    1. function Fish()
    2. {
    3.     var height : float = terrain.GetInterpolatedHeight(myT.position.x / terrain.size.x, myT.position.z / terrain.size.z);
    4.     status = HeronStatus.Walking;
    5.     var direction : Vector3;
    6.     var randomDir : Vector3 = Random.onUnitSphere;
    7.    
    8.     if(height > 40)
    9.     {
    10.         maxHeight = 40;
    11.         offsetMoveDirection = GetPathDirection(myT.position, randomDir);
    12.         yield WaitForSeconds(0.5);
    13.         if(velocity.magnitude > 0.01) direction = myT.right * (Random.value > 0.5 ? -1 : 1);
    14.     }
    15.     if(height > 38)
    16.     {
    17.         maxHeight = 38;
    18.         offsetMoveDirection = GetPathDirection(myT.position, randomDir);
    19.         yield WaitForSeconds(1);
    20.         if(velocity.magnitude > 0.01) direction = myT.right * (Random.value > 0.5 ? -1 : 1);
    21.     }
    22.     if(height > 36.5)
    23.     {
    24.         maxHeight = 36.5;
    25.         offsetMoveDirection = GetPathDirection(myT.position, randomDir);
    26.         yield WaitForSeconds(1.5);
    27.         if(velocity.magnitude > 0.01) direction = myT.right * (Random.value > 0.5 ? -1 : 1);
    28.     }
    29.     while(height > 35)
    30.     {
    31.         maxHeight = 35;
    32.         yield WaitForSeconds(0.5);
    33.         if(velocity.magnitude > 0.01) direction = myT.right * (Random.value > 0.5 ? -1 : 1);
    34.         offsetMoveDirection = GetPathDirection(myT.position, randomDir);
    35.         height = terrain.GetInterpolatedHeight(myT.position.x / terrain.size.x, myT.position.z / terrain.size.z);
    36.     }
    37.    
    38.     fishing = true;
    39.     status = HeronStatus.Walking;
    40.     yield WaitForSeconds(fishingTime / 3);
    41.     status = HeronStatus.Idle;
    42.     yield WaitForSeconds(fishingTime / 6);
    43.     status = HeronStatus.Walking;
    44.     yield WaitForSeconds(fishingTime / 3);
    45.     status = HeronStatus.Idle;
    46.     yield WaitForSeconds(fishingTime / 6);
    47.     fishing = false;
    48.    
    49.     maxHeight = 42;
    50. }
     
  4. Agilis

    Agilis

    Joined:
    Aug 13, 2009
    Posts:
    3
    Is the following right?

    function GetInterpolatedHeight(x : float, z : float) : float

    Get an interpolated height of the terrain at a given location.

    x,z coordinates are specified as normalized coordinates between 0 ... 1
     
  5. Helical

    Helical

    Joined:
    Mar 2, 2014
    Posts:
    50
    @Agilis Thank you for telling that its normalized values.

    I actually dont understand what it returns and normalized is what?

    Currently doing like so, and it seems to work


    Code (CSharp):
    1.     public void Flatten(Vector3 position, int width, int length){
    2.         int xBase = (int)(position.x - terrain.GetPosition().x);
    3.         int yBase = (int)(position.z - terrain.GetPosition().z);
    4.         TerrainData t = terrain.terrainData;
    5.  
    6.         float height = t.GetInterpolatedHeight((position.x - terrain.GetPosition().x)/t.heightmapWidth, (position.z - terrain.GetPosition().z)/t.heightmapHeight);
    7.         float normalizedHeight = height / t.size.y;
    8.         float[,] heights = new float[width, length];
    9.  
    10.         for(int i=0; i<width; ++i)
    11.             for(int j=0; j<length; ++j)
    12.                 heights[i,j] = normalizedHeight;
    13.  
    14.        
    15.         t.SetHeights(xBase, yBase, heights);
    16.     }