- Home /
Deform of terrain? (Real-time)
I'm making a game that includes mining, And I'm wondering how do you subtract terrain through scripting, and reset it to its original state after play testing? I look on youtube, but all of the videos include Adding height, not subtracting. Some features are: You mine where you look, (Left/Right) clicking; The derode is circular, in a small area, and I also need it to be addable, So you can keep mining forever. (Do all of this in Real time)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Deformation_Terrain : MonoBehaviour {
public Terrain terrainMain;
void OnGUI()
{
if(GUI.Button(new Rect(30, 30, 200, 30), "Change terrain height"))
{
// Get the terrains [x,y]
int xRes = terrainMain.terrainData.heightmapWidth;
int yRes = terrainMain.terrainData.heightmapHeight;
// Manipulate current heights value, and set it as an array using [x,y]
float[,] height = terrainMain.terrainData.GetHeights(0, 0, xRes, yRes);
// Change height, maximum height is 1, and lowest is 0.
height[10, 10] = 1f;
terrainMain.terrainData.SetHeights(0, 0, height);
}
}
}
Code - (Like requested)
If you know how to add height, then just put a $$anonymous$$us sign in front of the value... You might need to initialize the terrain heights to be 1 so that you can go down. Unity terrains have a $$anonymous$$imum and a maximum height so you can never use them to $$anonymous$$e 'forever' but you could set the position to be very very low down.
Alternatively you could go generate a mesh procedurally a bit like in a voxel system which would allow you to break the mesh up into smaller chunks and allow you to have quite good resolution 'locally' whilst not burning all your resources on a huge ~infinite~ world.
If you share some of the code you have managed then someone may be able to help fix/improve it.
I have done like you requested, I've added the code i used.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
RTS game Any help about Hide mesh or destroy trees Terrain? 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Hello! I have a problem. How can I make a script to remove the grass when a cube is falling? 0 Answers