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. Dismiss Notice

Making terrain flat under an object [Script][Probleme with script]

Discussion in 'Editor & General Support' started by mathias234, Jul 1, 2014.

  1. mathias234

    mathias234

    Joined:
    Sep 9, 2012
    Posts:
    239
    Hey guys! I am having this probleme with a script i found and edited a little i am trying to use a raycast to find the distance between the object and the terrain and then set the terrain flat at that location it kinda works but i only get high pillars i guess that i need to do something with the hit.distance i get here is the script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Straighttheterrain : MonoBehaviour {
    5.     Terrain terr; // terrain to modify
    6.     int hmWidth; // heightmap width
    7.     int hmHeight; // heightmap height
    8.    
    9.     int posXInTerrain; // position of the game object in terrain width (x axis)
    10.     int posZInTerrain; // position of the game object in terrain height (y axis)
    11.    
    12.     int size = 100; // the diameter of terrain portion that will raise under the game object
    13.     float desiredHeight = 0; // the height we want that portion of terrain to be
    14.  
    15.     bool updated;
    16.    
    17.     void Start () {
    18.         terr = Terrain.activeTerrain;
    19.         hmWidth = terr.terrainData.heightmapWidth;
    20.         hmHeight = terr.terrainData.heightmapHeight;
    21.        
    22.     }
    23.    
    24.     void Update () {
    25.         if(!updated) {
    26.             RaycastHit hit;
    27.             if (Physics.Raycast(transform.position, -Vector3.up, out hit)){
    28.                 desiredHeight = hit.distance;
    29.                 Debug.Log(hit.transform.name + " " + hit.distance.ToString());
    30.             }
    31.            
    32.  
    33.             // get the normalized position of this game object relative to the terrain
    34.             Vector3 tempCoord = (transform.position - terr.gameObject.transform.position);
    35.             Vector3 coord;
    36.             coord.x = tempCoord.x / terr.terrainData.size.x;
    37.             coord.y = tempCoord.y / terr.terrainData.size.y;
    38.             coord.z = tempCoord.z / terr.terrainData.size.z;
    39.             // get the position of the terrain heightmap where this game object is
    40.             posXInTerrain = (int) (coord.x * hmWidth);
    41.             posZInTerrain = (int) (coord.z * hmHeight);
    42.            
    43.             // we set an offset so that all the raising terrain is under this game object
    44.             int offsetX = (int)transform.localScale.x / 2;
    45.             int offsetZ = (int)transform.localScale.z / 2;
    46.  
    47.            
    48.             // get the heights of the terrain under this game object
    49.             float[,] heights = terr.terrainData.GetHeights(posXInTerrain-offsetX,posZInTerrain-offsetZ, (int)transform.localScale.x, (int)transform.localScale.z);
    50.            
    51.             // we set each sample of the terrain in the size to the desired height
    52.             for (int i=0; i<(int)transform.localScale.z; i++)
    53.                 for (int j=0; j<(int)transform.localScale.x; j++)
    54.                     heights[i,j] = desiredHeight;
    55.  
    56.  
    57.            
    58.             // set the new height
    59.             terr.terrainData.SetHeights(posXInTerrain-offsetX,posZInTerrain-offsetZ,heights);
    60.             updated = true;
    61.         }
    62.     }
    63. }
    64.  
     
  2. gbagiryan

    gbagiryan

    Joined:
    May 7, 2020
    Posts:
    10
    Hey man, I realise it's been a while but did you find a solution? I need the exact same thing and can't find anything simmilar