Search Unity

Unity Terrain Change - Real Time

Discussion in 'Scripting' started by stevedarby02, Jan 10, 2014.

  1. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    Hi,

    I've copied this from else well (beyond my technical knowledge!), but could someone help me add a 'max' variable to the height in which the player can increase/decrease the terrain?

    There are other things i want to do like restrict where they can update terrain, but i think i can do that.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class tut_02_terrain : MonoBehaviour
    6. {
    7.     public Terrain myTerrain;
    8.     int xResolution;
    9.     int zResolution;
    10.     float[,] heights;
    11.    
    12.     void Start()
    13.     {
    14.         xResolution = myTerrain.terrainData.heightmapWidth;
    15.         zResolution = myTerrain.terrainData.heightmapHeight;
    16.         heights = myTerrain.terrainData.GetHeights(0,0,xResolution,zResolution);
    17.     }
    18.    
    19.     void Update()
    20.     {
    21.         if(Input.GetMouseButtonDown(0))
    22.         {
    23.             RaycastHit hit;
    24.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    25.             if(Physics.Raycast(ray, out hit))
    26.             {
    27.                 raiseTerrain(hit.point);
    28.             }
    29.         }
    30.         if(Input.GetMouseButtonDown(1))
    31.         {
    32.             RaycastHit hit;
    33.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    34.             if(Physics.Raycast(ray, out hit))
    35.             {
    36.                 lowerTerrain(hit.point);
    37.             }
    38.         }
    39.     }
    40.    
    41.     private void raiseTerrain(Vector3 point)
    42.     {
    43.         int terX =(int)((point.x / myTerrain.terrainData.size.x) * xResolution);
    44.         int terZ =(int)((point.z / myTerrain.terrainData.size.z) * zResolution);
    45.         float y = heights[terX,terZ];
    46.         y += 0.001f;
    47.         float[,] height = new float[1,1];
    48.         height[0,0] = y;
    49.         heights[terX,terZ] = y;
    50.         myTerrain.terrainData.SetHeights(terX, terZ, height);
    51.     }
    52.    
    53.     private void lowerTerrain(Vector3 point)
    54.     {
    55.         int terX =(int)((point.x / myTerrain.terrainData.size.x) * xResolution);
    56.         int terZ =(int)((point.z / myTerrain.terrainData.size.z) * zResolution);
    57.         float y = heights[terX,terZ];
    58.         y -= 0.001f;
    59.         float[,] height = new float[1,1];
    60.         height[0,0] = y;
    61.         heights[terX,terZ] = y;
    62.         myTerrain.terrainData.SetHeights(terX, terZ, height);
    63.     }
    64. }
    65.  
    Thanks
     
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Try this

    PHP:
    using UnityEngine;
    using System.Collections;

    public class 
    tut_02_terrain MonoBehaviour
    {
        public 
    Terrain myTerrain;
        public 
    float maximumHeight 100// line added
        
    public float minimumHeight 0// line added
        
    int xResolution;
        
    int zResolution;
        
    float[,] heights;
        
        
    void Start()
        {
            
    xResolution myTerrain.terrainData.heightmapWidth;
            
    zResolution myTerrain.terrainData.heightmapHeight;
            
    heights myTerrain.terrainData.GetHeights(0,0,xResolution,zResolution);
        }

        
    void Update()
        {
            if(
    Input.GetMouseButtonDown(0))
            {
                
    RaycastHit hit;
                
    Ray ray Camera.main.ScreenPointToRay(Input.mousePosition);
                if(
    Physics.Raycast(rayout hit))
                {
                    
    raiseTerrain(hit.point);
                }
            }
            if(
    Input.GetMouseButtonDown(1))
            {
                
    RaycastHit hit;
                
    Ray ray Camera.main.ScreenPointToRay(Input.mousePosition);
                if(
    Physics.Raycast(rayout hit))
                {
                    
    lowerTerrain(hit.point);
                }
            }
        }

        private 
    void raiseTerrain(Vector3 point)
        {
            
    int terX =(int)((point.myTerrain.terrainData.size.x) * xResolution);
            
    int terZ =(int)((point.myTerrain.terrainData.size.z) * zResolution);
            
    float y heights[terX,terZ];
            
    Mathf.Min(.001fmaximumHeight myTerrain.terrainData.size.y); // line changed
            
    float[,] height = new float[1,1];
            
    height[0,0] = y;
            
    heights[terX,terZ] = y;
            
    myTerrain.terrainData.SetHeights(terXterZheight);
        }

        private 
    void lowerTerrain(Vector3 point)
        {
            
    int terX =(int)((point.myTerrain.terrainData.size.x) * xResolution);
            
    int terZ =(int)((point.myTerrain.terrainData.size.z) * zResolution);
            
    float y heights[terX,terZ];
            
    Mathf.Max(.001fminimumHeight myTerrain.terrainData.size.y); // line changed
            
    float[,] height = new float[1,1];
            
    height[0,0] = y;
            
    heights[terX,terZ] = y;
            
    myTerrain.terrainData.SetHeights(terXterZheight);
        }
    }
     
  3. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    Thanks, that's great.

    Works perfectly

    Thanks again
     
    Last edited: Jan 12, 2014