- Home /
Real-time terrain editor?
Ok so I have this code:
using UnityEngine;
using System.Collections;
public class TerrainEditing : MonoBehaviour {
private Ray ray;
private RaycastHit hit;
public Terrain terrain;
void Update () {
if (Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100))
{
Vector3 pos = hit.point;
terrain.terrainData.SetHeights((int)pos.x, (int)pos.y, new float[100,100]);
}
}
}
}
But for some reason, this doesn't work! I just want to click anywhere on the terrain and have it raise to what height I want.
Can anyone see something wrong with this code?
Thanks,
Alex
Answer by Dreamblur · Jun 23, 2011 at 11:45 PM
You're trying to access terrain which hasn't been initialized. What you should be doing instead is creating a new TerrainData, using the methods in that TerrainData to initialize the terrain settings, and then calling Terrain.CreateTerrainGameObject(the newly created TerrainData).
It's easier to just instantiate a terrain prefab though. 0_0
Answer by Alex_S · Jun 24, 2011 at 12:10 AM
What if I already have a terrain in game, though? I linked my public terrain variable to the active terrain in my scene.
Excuse my stupidity, I'm still trying to get used to Unity's 'coding'. :p
Terrain.Flush
function Flush () : void
Description
Flushes any change done in the terrain so it takes effect.
Sorry, this is my first time using answers... lol... I forgot to read. :p And thanks Dreamblur. I'll try it and see what happens.