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