我已经通过编辑器在地形上放置了一些树木。我有一个通过树木的物体,当这个物体与某些树碰撞时,我想改变它的顶点。
void OnTriggerEnter (Collider col) {
DestroyableTree tree = col.gameObject.GetComponent<DestroyableTree>();
if (tree == null)
return;
Mesh mesh = tree.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
for (var i = 0; i < 100; i++){ // 100 just random number, as well as 50 bellow
vertices[i].x = 50;
vertices[i].z = 50;
}
mesh.vertices = vertices;
mesh.RecalculateBounds ();
mesh.RecalculateNormals ();
tree.GetComponent<MeshFilter> ().mesh = mesh;
//tree.UpdadeMesh(tree, mesh);
//tree.Delete();
}
DestroyableTree来自http://rene.klacan.sk/unity3d/games/2014/10/28/destroyable-terrain-trees-in-unity/
取消注释时tree.Delete()
让我确定我正在尝试编辑正确的树。
当我调试时,我可以看到网格的顶点已经改变,但为什么它们不会在游戏场景的运行期间更新?
编辑:这可能是因为我正在访问胶囊组件的网格而不是树本身。我如何访问地形上的树网格(不一定是上面显示的代码)?