This site uses strictly necessary cookies. More Information

X- 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 coming 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

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.