- Home /
How to move specified vertices to a specific place/ specific direction
I have seen this page: http://docs.unity3d.com/ScriptReference/Mesh.html
this explains how to move some vertices and animate then but i do not fully understand it. I hear that the second script is the better one to move vertices however, i dont understand it.
Code:
function Update () { var mesh : Mesh = GetComponent(MeshFilter).mesh; var vertices : Vector3[] = mesh.vertices; var normals : Vector3[] = mesh.normals; for (var i = 0; i < vertices.Length; i++) vertices[i] += normals[i] * Mathf.Sin(Time.time);
mesh.vertices = vertices;
}
I would like to detect some vertices through collision and move them accordinly... However, i don not know 1. How to detect those vertices 2. How to move that vertices to a specific place
This is a complicated problem that does not have a perfect solution. One appraoch: In a collison, you get a contact points and normals. You can use these with Collider.Raycast() to generate a RaycastHit for the point. This will give you the triangle index of the triangle that contains the point. From the triangle index, you can generate the three vertices in the vertices array that define triangle. Note that these vertices will be in local space, so you will need to some converting to/from world/local space in order to figure out the new placement of the vertices.
Another solutions is to compare the contact points with the vertices and move the vertices based on distance to the contact points. Since a mesh is in local space, you will need to convert to/from local/world space. See Transform.TransformPoint(), Transform.InverseTransformPoint(), Transform.TransformDirection(), Tansform.InverseTransformDirection().
Thanks Rob, I understand where you are co$$anonymous$$g from but i don't understand how to move certain vertices from the collision point. I can properly figure out how to find the collisions but i don't know how to move the vertices... Any help?
Your answer
![](https://koobas.hobune.stream/wayback/20220613151314im_/https://answers.unity.com/themes/thub/images/avi.jpg)