Programmatic control of Terrain object?

Hi,

I’m new to Unity. First off I have to say how crazily easy it is to build games and physically-modeled scenes with Unity. I’ve been doing software development for 20 years and have coded some realtime music and physics projects in the past and what’s possible here is amazing.

I’m wondering if the Terrain object can be controlled programmatically? I’m building a simple game that relies on the user being able to temporarily deform a surface and I thought the Terrain object and tools would be great, but I’d need to be able to script the changes in response to user input during gameplay. Thanks for any suggestions.

-M

The TerrainData class is what you’re looking for. Use Terrain.activeTerrain.terrainData to get a reference to the one providing the data for the currently active terrain. Alternatively you can also just make a public variable and drop the automatically generated TerrainData asset for your terrain in the inspector field. Other ways of modifying a 3d surface would be procedural mesh generation or a custom vertex shader that translates vertices based on a read/write enabled texture. Depending on how you use it those solutions might be preferable as they are more lightweight and give you more control. The Terrain system is optimized for extremely large surfaces and does a lot of things you might not need.