Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can I generate TerrainData (and a Terrain Collider) for a non-standard terrain?

Discussion in 'Physics' started by bettywhite, Jun 6, 2023.

  1. bettywhite

    bettywhite

    Joined:
    Oct 22, 2019
    Posts:
    13
    Hi there,

    I have created a procedurally-generated terrain made up of equilateral triangles by adapting the Sebastian Lague technique. It looks like this:

    https://imgur.com/a/rcnoHPO

    You can see in the third image that I have produced the equilateral triangles by offsetting every second row on the z axis by half the width of a triangle, which I believe may cause some issues for creating TerrainData.

    The problem I have run into is trying to generate TerrainData for the Terrain object so that I can pass it to the TerrainCollider and raycast to the terrain to place objects, as well as having characters walk atop it. When I try it unfortunately generates a garbled pink terrain.

    The methodology I have been using to generate the TerrainData is as follows:

    Code (CSharp):
    1.     TerrainData GenerateTerrainData(TerrainData terrainData){
    2.         terrainData.heightmapResolution = coordinateMapper.mapGenerator.mapWidth + 1;
    3.         terrainData.size = new Vector3(coordinateMapper.mapGenerator.mapWidth, coordinateMapper.mapGenerator.mapHeight, coordinateMapper.mapGenerator.mapWidth);
    4.         terrainData.SetHeights(0,0,coordinateMapper.mapGenerator.noiseMap);
    5.  
    6.         return terrainData;
    7.     }
    8.  
    9.     public void AssignTerrainData(){
    10.         terrain.terrainData = new();
    11.         terrain.terrainData.name = "Procedural Test Mesh Data";
    12.         terrain.terrainData = GenerateTerrainData(terrain.terrainData);
    13.         terrain.gameObject.GetComponent<TerrainCollider>().terrainData = terrain.terrainData;
    14.     }
    Can anyone tell me how to correctly produce the TerrainData? Or if indeed it is possible for a non-standard (i.e. non-rectangular) terrain?

    Thanks for your time.