- Home /
How to move the lowest vertex of a humanoid mesh on y-axis?
My problem is that I spawn humanoid with different textures, which can influence the y position, thus I wander how is it possible to change the lowest vertex of a mesh somehow through script in order to set it y position to 0.
Answer by Klarzahs · Jan 09, 2019 at 10:59 AM
Hi,
You can access a mesh and iterate over every vertex to find the lowest one. Accessing can be done with
Vector3[] verts = gameobject.GetComponent<MeshFilter>().mesh.vertices;
See here for more info. After you changed your vertex, you'll have to reassign the vertices to the mesh and recalculate normals / bounds. Its probably best not to do it in every update step ;)
I am trying to code it like this, however I always get an array out of range error when i assign verts[0]
//FINDING LOWEST VERTEX
Vector3[] verts = GetComponent<$$anonymous$$eshFilter>().mesh.vertices;
lowestVertex = verts[0];
for (int i = 0; i < verts.Length; i++)
{
if (verts[i].y < lowestVertex.y)
lowestVertex = verts[i];
}
Debug.Log("lowest vertex is" + lowestVertex);
Are you maybe using a shared mesh? Try GetComponent().shared$$anonymous$$esh; It otherwise should work, can you step through it in VS and have a look at the vertices? Oh and check if you are grabbing the correct $$anonymous$$eshFilter and if it has the correct mesh linked
Likning the correct mesh totally solved my issue, thank you!