I have placed some trees onto the terrain via editor. I have an object which is passing throuh trees and when this object collides with certain tree I want to change its vertices.
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 is from http://rene.klacan.sk/unity3d/games/2014/10/28/destroyable-terrain-trees-in-unity/
When uncommented tree.Delete()
lets me be sure that I was trying to edit correct tree. When I debug I can see that vertices of mesh have changed but why they won't update during runtime of game scene?
EDIT: it is probably because I am accesing capsule component's mesh but not of the tree itself. Still how do I access mesh of trees on the terrain (not necessarily with the code displayed above)?
Using code provided in the link above and having it modified a bit worked. Although it didn't solve the very root issue why I had to ask this question in the first place :(
So here it is how I did it, simple: in the class DestroyableTree
that René Klačan wrote I added additional field public TreeInstance treeInstance;
which gets initialized after marked statement (in the picture bellow) by adding tree.treeInstance = treeInstance;
: After that I had to reach mesh of the tree using tree.treeInstance.GetComponent<MeshFilter>().mesh;
in my code at the beggining of the question. I hope someone will find this useful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.