Game development  - 
 
Anyone know of any examples or tutorials on how to modify terrain by code? I know its possible...
1
Patrick Q's profile photoChad Jenkins's profile photo
4 comments
 
I suppose it depends on exactly what it is you want to do with the terrain. I've not been able to find full tutorials. However, I do recall seeing some information on Unity Answers that seemed useful.

http://answers.unity3d.com/questions/11093/modifying-terrain-height-under-a-gameobject-at-run.html

http://answers.unity3d.com/questions/9248/how-to-translate-world-coordinates-to-terrain-coor.html

http://docs.unity3d.com/Documentation/ScriptReference/TerrainData.html

Hopefully that will give you a decent starting point. The general procedure appears to be to get the array of heights using terrainData.GetHeights. Alter them as you wish, and save it back using terrainData.SetHeights.
 
Awesome, thanks! My intent is to dynamically generate terrain using Perlin noise. I used to do it in Aviary's node-based image effects tool, but I'd like to figure out how to do it at runtime within Unity.
 
Ah, okay. You may want to have a look at the port of LibNoise to Unity then. Once you've generated an appropriate array of height values, you should be able to just save them back to the terrain data structure. You can even use a good noise library to ensure the edge of your cells are seamless. (If you want to tile terrains for super massive areas)

http://forum.unity3d.com/threads/68764-LibNoise-Ported-to-Unity

Libnoise also has some excellent tutorials on creating noise based terrain features, if you're interested. Even though it's c++ most of the concepts can be easily ported over to C# or another language.

http://libnoise.sourceforge.net/tutorials/
Add a comment...