Search Unity

how to setup terrain via code?

Discussion in 'Getting Started' started by Haruji, Jun 21, 2017.

  1. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
    I'm learning about terrain.
    I don't know how to make my terrain change along the time.
    I want to make some terrain collapses at a random time and some elevates too.
    but i don't know how?
    could someone help me?
    Thanks,
    Haruji.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,433
    Kiwasi likes this.
  3. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    207
    When using TerrainData.SetHeights(), know that it can slow down the game a lot if you have a large terrain so if possible you don't want to call it every frame as it's going to kill performance.

    Tiling several smaller terrains can help, but also makes it more complicated to keep them seamless.
     
    Kiwasi likes this.
  4. dariannikookar

    dariannikookar

    Joined:
    Feb 1, 2017
    Posts:
    3
    If it's a "simple" game, why not try using prefabs as a ground base (could even be on top of the Terrain, and by using clones it can save you a lot of memory) and transform the heights of them?
    But I'm just a beginner when it comes to the Unity realm, so I could be making no sense at all :)
     
  5. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
    Thanks a lot for your tips.
    lately, i was busy so can't reply.
    Sorry for that.
    I have a lot of thing in my head now like :
    - How to setup ragdoll by code.
    - How to call a prefab ( ground prefab as it's mentioned above, modeling ....)
    I am programmer before.
     
  6. dariannikookar

    dariannikookar

    Joined:
    Feb 1, 2017
    Posts:
    3
    No worries :)
    I've just started with Unity3D myself and there are so many ways to one sollution.

    As => mgear <= stated you could use the:

    public Terrain thisTerrain;
    private float[,] sampleHeights;
    private float[,] newSampleHeights

    sampleHeights = thisTerrain.terrainData.GetHeights(startX, startY, xSamples, ySamples);
    (example: sampleHeight = thisTerrain.terrainData.GetHeights(0,0, 10, 10);)

    the height then returns a float[,] (array) with a range(0.0f, 1,0f) with 1.0f being the highest.
    you could now manipulate the height array and set the new heights.
    You could also get single height(s) with: GetHeight and SetHeight and a:
    for(int y=0; y < thisTerrain.terrainData.heightmapResolution; y++){for(int x=0; x < thisTerrain.terrainData.heightmapResolution; x++){ ... } } etc...

    thisTerrain.terrainData.SetHeights(startX, startY, newSampleHeights);
    thisTerrain.Flush();

    As => eXonius <= stated...
    The 'Terrain' object is normally a static object that uses baking for lighting. You could choose to NOT use your Terrain as a static object, so that there is NO baking during run time. That means ALL the lighting on the Terrain will be in realtime instead of pre-rendered (baked) lighting.
    - Changes done to the Terrain's height are permanent (in a way, that you can't undo them by default; you could save the previous heights in a scriptable object)

    But the "prefab" or just a simple mesh on top of a Terrain (maybe even with sand/dirt textures) could be less complex :)

    With that said...

    I'm currently playing with making a simple multi tiled terrain editor (I know that there are some out there already, but just want to make one myself :) .
    Done: instantiate multiple tiles // set the neighbors of each tile // average off the 1 sample seams // set the textures (splats) on all of the terrains // change terrain height with a perlin noise.
    Not Done: texture painting currently done per tile => should be done as one big tile, than bring it back to fix round off errors.
    So if I fix the textures (should be done next week (I hope! :)) I'll post it. There are some examples in it you could use as it uses the Get- and SetHeights.

    Cheers