- Home /
lowering Terrain at run-time, C#
Hi Everyone, I'm working with a code that lowers my Terrain from a tool at run-time, which is working fine. I used a sample code that I found online and tweaked it for what I'm doing, but I want to change the size of the hole. if anyone can have any suggestions it would be greatly appreciated.
using UnityEngine;
using UnityEngine;
using System.Collections;
using System.Collections;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
public class TestShovel : MonoBehaviour {
bool shouldDraw = false;
public Terrain myTerrain;
TerrainData tData;
int xResolution;
int zResolution;
float[,] heights;
public float speed = 0.05f;
string speedString;
public Transform myTransform;
// Use this for initialization
void Start () {
myTransform = transform;
speedString = speed.ToString();
tData = myTerrain.terrainData;
xResolution = tData.heightmapWidth;
zResolution = tData.heightmapHeight;
heights = tData.GetHeights (0, 0, xResolution, zResolution);
}
// Update is called once per frame
void Update () {
speed = Convert.ToSingle (speedString);
if (Input.GetMouseButton (0)) {
lowerTerrain(myTransform.position);
}
}
private void raiseTerrain(Vector3 point){
int mouseX = (int)((point.x / tData.size.x) * xResolution);
int mouseZ = (int)((point.z / tData.size.z) * zResolution);
float[,] modHeights = new float[1, 1];
float y = heights [mouseX, mouseZ];
y += (speed / 10) * Time.deltaTime;
if (y > tData.size.y)
y = tData.size.y;
modHeights [0, 0] = y;
heights [mouseX, mouseZ] = y;
tData.SetHeights (mouseX, mouseZ, modHeights);
}
private void lowerTerrain(Vector3 point){
int mouseX = (int)((point.x / tData.size.x) * xResolution);
int mouseZ = (int)((point.z / tData.size.z) * zResolution);
float[,] modHeights = new float[0.5f, 0.5f];
float y = heights [mouseX, mouseZ];
y -= (speed / 10) * Time.deltaTime;
if (y < 0.0f)
y = 0.0f;
modHeights [0, 0] = y;
heights [mouseX, mouseZ] = y;
tData.SetHeights (mouseX, mouseZ, modHeights);
}
}
Comment