Sorting through the triangles of a NavMesh to get a single triangle from a specific area?
Okay so I undertstand how I should be doing this:
NavMeshTriangulation.areas is an int[], each element corresponds to a triangle on the Navesh, with each int indicating the area (NavMeshTriangulation.areas[16] could be part of area 5 for example).
NavMeshTriangulation.indices is an int[], in groups of 3 they correspond to the indices of the
vertices (NavMeshTriangulation.indices[0] through [5] would be { 0, 1, 2, 0, 2, 3 } for a square.NavMeshTriangulation.vertices is a Vector3[] containing the vectors of all the verts.
So to draw a ray for each vertex of an area I would:
Loop through the area[] to see if the index matches my area (5), store the index in a List or something.
Loop through the indices[] (using the stored area ints to make sure I'm looking at the right triangle), store the index in another List, plus the two indexes after (to make a triangle).
Finally use the stored values in the second list to Debug.DrawRay() the vertices.
However I've tried doing this and it keeps coming out wrong. I can draw a ray for every vertex on the NavMesh no problem, when I try and do it for a specific area it draws the rays on points other than that.
I'm probably just messing up a really silly calculation somewhere but if I do it by hand it all checks out.
Here's a very broken down version of my code that I made to make sure everything looked good: https://hatebin.com/bjrijwiveq
Thank you for any help!
Answer by unity_ZlNDOyAefFicCQ · Mar 04, 2021 at 05:52 PM
Turns out that I was just getting confused and mixing up some of the vertex Vectors, here's the basic code to put a ray on each point of a specific area (In this case area 5):
for (int i = 0; i < navMeshTriangulation.areas.Length; i++) { if (navMeshTriangulation.areas[i] == 5) { int j = i * 3; for (int x = 0; x < 3; x++) { Debug.DrawRay(navMeshTriangulation.vertices[navMeshTriangulation.indices[j + x]], Vector3.up, Color.blue, 30.0f); } } }
It would be really usefull if you could somehow see the triangles with their index in the editor, but this shall suffice.
Can you please explain indices? I got a task to draw all outline vertex and i am not sure what are these indices and vertices about..
Your answer
![](https://koobas.hobune.stream/wayback/20220613030134im_/https://answers.unity.com/themes/thub/images/avi.jpg)