Search code examples
c#unity-game-enginerefreshterrain

Terrain refresh lag unity c# - How effectively refresh the terrain?


In the game, players are able to chop down trees. I then instantiate a falling tree in it's spot.

I remove the tree from the terrain list and refresh the terrain like so:

        var treeInstancesToRemove = new List<TreeInstance>(terrain.treeInstances);
        treeInstancesToRemove.RemoveAt(closestTreeIndex);
        terrain.treeInstances = treeInstancesToRemove.ToArray();

        // I refresh the terrain so the collider gets removed...
        float[,] heights = terrain.GetHeights(0, 0, 0, 0);
        terrain.SetHeights(0, 0, heights);

The terrain is VERY LARGE... This means that whenever a tree is chopped the game freezes for a few seconds and then resumes (as it refreshes). Is there a faster or more effective way I could take a look at? Having a freeze after every tree you chop down is not quite ideal?

THANKS A LOT IN ADVANCE!


Solution

  • The best thing I can suggest is having the world split into chunks that you can update separately. Either that, or have the collider update in a separate thread from the main one.