Unity Terrain Change - Real Time

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.

using UnityEngine;
using System.Collections;

public class tut_02_terrain : MonoBehaviour
{
	public Terrain myTerrain;
	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(ray, out hit))
			{
				raiseTerrain(hit.point);
			}
		}
		if(Input.GetMouseButtonDown(1))
		{
			RaycastHit hit;
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if(Physics.Raycast(ray, out hit))
			{
				lowerTerrain(hit.point);
			}
		}
	}
	
	private void raiseTerrain(Vector3 point)
	{
		int terX =(int)((point.x / myTerrain.terrainData.size.x) * xResolution);
		int terZ =(int)((point.z / myTerrain.terrainData.size.z) * zResolution);
		float y = heights[terX,terZ];
		y += 0.001f;
		float[,] height = new float[1,1];
		height[0,0] = y;
		heights[terX,terZ] = y;
		myTerrain.terrainData.SetHeights(terX, terZ, height);
	}
	
	private void lowerTerrain(Vector3 point)
	{
		int terX =(int)((point.x / myTerrain.terrainData.size.x) * xResolution);
		int terZ =(int)((point.z / myTerrain.terrainData.size.z) * zResolution);
		float y = heights[terX,terZ];
		y -= 0.001f;
		float[,] height = new float[1,1];
		height[0,0] = y;
		heights[terX,terZ] = y;
		myTerrain.terrainData.SetHeights(terX, terZ, height);
	}
}

Thanks

Try this

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(ray, out hit))
            {
                raiseTerrain(hit.point);
            }
        }
        if(Input.GetMouseButtonDown(1))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if(Physics.Raycast(ray, out hit))
            {
                lowerTerrain(hit.point);
            }
        }
    }

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

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

Thanks, that’s great.

Works perfectly

Thanks again