Skip to content

Instantly share code, notes, and snippets.

@jonstvns
Last active August 29, 2015 13:56
Show Gist options
  • Save jonstvns/8951191 to your computer and use it in GitHub Desktop.
Save jonstvns/8951191 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class ClickTerrain : MonoBehaviour
{
void Start()
{
var terrain = Terrain.activeTerrain;
var td = terrain.terrainData;
var resX = td.heightmapWidth;
var resY = td.heightmapHeight;
var strength = 1f;
Vector3 clickPos = Vector3.zero;
Ray ray = Camera.main.ViewportPointToRay(Vector3.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var pos = clickPos - terrain.GetPosition();
pos.y = pos.z;
pos.z = 0;
pos = new Vector2(pos.x / td.size.x, pos.y / td.size.z);
pos = new Vector2(pos.x * td.heightmapWidth, pos.z * td.heightmapHeight);
float[,] heights = td.GetHeights(0, 0, resX, resY);
//heights[(int)pos.x, (int)pos.y] = strength;
for (int x = 0; x < resX; x++)
{
for (int y = 0; y < resY; y++)
{
heights[x, y] = Mathf.Clamp((heights[x, y] + 0.01f), 0f, 1f);
}
}
td.SetHeights(0, 0, heights);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment