- Home /
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
I don't understand what you are trying to do between line 40 and 47... dist is always going to be 1...
That section is when I attempted to implement the smooth increase by myself by calculating the distance from the centre of the terrain to raise and then decreasing the multiplier the further away the function gets from the center.
For example, if the point is 3 units away from the center it will be increased by 1/3 of the amount of the point at the center.
Am I doing this wrong?
Do you want to recreate the builtin terrainbrush ingame or do you just want the terrain to be a big hill?
I would like to recreate the terrain brush effect (ideally)
Answer by OrbitGames · Aug 31, 2014 at 02:40 PM
(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)
Your answer
Follow this Question
Related Questions
Paint Terrain Texture From Vector3 List 0 Answers
lowering Terrain at run-time, C# 0 Answers
How to make trees in a terrain transparent in runtime? 0 Answers
How to add new baked lightmaps to lightmap array? 2 Answers
Modify a mesh at runtime 0 Answers