Hi! I’m not enough experienced in editing vertices.
I’m working on cutting tree.
My plan how to do it is in attached file “destruct tree.png”.
Problem is: I don’t know how to remove vertices like on image.
So, How I began:
void Update () {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
if (Physics.Raycast(new Ray(transform.position, transform.forward), out hit)) {
if (hit.transform.tag == "Tree") {
Vector3 hitPos = hit.point - hit.transform.position;
MeshFilter meshFilter = hit.transform.gameObject.GetComponent<MeshFilter>();
Mesh mesh = meshFilter.mesh;
List<Vector3> vertices = new List<Vector3>(mesh.vertices);
Vector3 temp = Vector3.zero;
float distance = 10000;
foreach (Vector3 vec in vertices.Where(vec => Vector3.Distance(vec, hitPos) < distance)) {
temp = vec;
distance = Vector3.Distance(temp, hitPos);
}
//TODO: Find by index and then remove
vertices.Remove(temp);
mesh.vertices = vertices.ToArray();
Debug.Log("NearTo " + hitPos + " is: " + temp + " -- DISTANCE: " + distance);
}
}
}
}
What I’m trying to do:
Get vertices at position where is hit and then remove them.
Can anyone explain to me what I do wrong? Thanks.