2014-12-01 55 views
0

在遊戲中,玩家可以砍樹。然後,我實例化一棵倒下的樹在它的位置。地形刷新滯後統一c# - 如何有效刷新地形?

我從地形列表中移除該樹並刷新地形就像這樣:

 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); 

地形是非常大的。這意味着只要一棵樹被砍遊戲凍結幾秒鐘,然後繼續(因爲它刷新)。有沒有更快或更有效的方式我可以看看?每砍掉一棵樹後凍結都不太理想?

感謝您的提前!

回答

0

我可以建議的最好的事情是讓世界分裂成可以單獨更新的塊。要麼是這樣,要麼將碰撞器更新爲與主碰撞器不同的線程。

+0

我喜歡chunks的想法,我只是沒有想法如何在Unity中做到這一點!我會做一些研究... – 2014-12-01 19:09:46

0
float hmWidth = grav.currentTerrain.terrainData.heightmapWidth; 
    float hmHeight = grav.currentTerrain.terrainData.heightmapHeight; 
    // get the normalized position of this game object relative to the terrain 
    Vector3 tempCoord = (transform.position - grav.currentTerrain.gameObject.transform.position); 
    Vector3 coord; 
    coord.x = tempCoord.x/grav.currentTerrain.terrainData.size.x; 
    coord.y = tempCoord.y/grav.currentTerrain.terrainData.size.y; 
    coord.z = tempCoord.z/grav.currentTerrain.terrainData.size.z; 
    // get the position of the terrain heightmap where this game object is 
    int posXInTerrain = (int)(coord.x * hmWidth); 
    int posYInTerrain = (int)(coord.z * hmHeight); 
    // we set an offset so that all the raising terrain is under this game object 
    //int offset = size/2; 

    // get the heights of the terrain under this game object 
    float[,] heights = grav.currentTerrain.terrainData.GetHeights(posXInTerrain, posYInTerrain, 1, 1); 
    grav.currentTerrain.terrainData.SetHeights(posXInTerrain, posYInTerrain, heights); //THIS CHANGES TERRAIN FOR GOOD 
+0

這個答案可以通過添加一些非代碼內容來改善。 – Thomas 2015-10-07 19:37:38