Smooth Terrain Increase

Hi guys,

I have written a script for modifying the terrain level at runtime. Unfortunately, however, the terrain increases as a square instead of the desirable smooth circle, like in the built in terrain modification script.

using UnityEngine;
using System.Collections;

public class TerrainEdit : MonoBehaviour {

	public Terrain terr;
	public int size = 20;
	// Use this for initialization
	void Start() {

	}
	
	// Update is called once per frame
	void Update() {
		if (Input.GetButton("Fire1") | Input.GetButton("Fire2")) {
			editTerrain ();
		}
	}

	void editTerrain() {
		int posX = (int)(((transform.position.x/terr.terrainData.size.x)*terr.terrainData.heightmapWidth)-(size/2f));
		int posZ = (int)(((transform.position.z/terr.terrainData.size.z)*terr.terrainData.heightmapWidth)-(size/2f));
		int xResolution = size;
		int zResolution = size;
		float[,] heights = terr.terrainData.GetHeights (posX, posZ, xResolution, zResolution);

		float increase;

		if (Input.GetButton("Fire2")) {
			increase = -0.001f;
		} else {
			increase = 0.001f;
		}

		int centX = posX - (size / 2);
		int centZ = posZ - (size / 2);

		for (int z = 0; z < zResolution; z++) {
			for (int x=0; x < xResolution; x++) {
				float idealHeight = heights[x, z] + increase;
				float dist = 1;
				float mult = 1;
				if (dist > 1) {
					mult = 1/dist;
				} else if (dist > size/4) {
					mult = 0;
				}
				heights[x, z] = idealHeight * mult;
			}
		}

		terr.terrainData.SetHeights(posX, posZ, heights);
	}
}

Any help would be greatly appreaciated

(outside the for loops)check where the mouse is clicking(use raycasting) on the terrain

find world coordinates of terrain coordinate(don’t know how to call it)(tutorial to do the reversed thing here)

get the distance between the x & z coordinates of the upper two

then (if the distance is between 0 and max distance)
heights[i,j] +=(1-(maxdistance / distance) * heaviness) to the height;

This will result in a cone like rise…

I’m not sure it will work but you can try this formula instead:

heights[i,j] += 2*(distance/maxdistance)^3-3*(distance/maxdistance)^2+1;

hope this helps

(everything is untested)