0
\$\begingroup\$

I have gameobjects in my scene called icospheres that I have made in Blender, and I am trying to get the coordinates of their vertices. Below is an image of my scene; each triangle is a separate gameobject, blender mesh and is called an icosphere:

Here is the code I am using:

    var meshFilter = icosphere.GetComponent<MeshFilter>();
    var vertices = meshFilter.mesh.vertices;
    GameObject.Find("Fauna Manager").GetComponent<FaunaManager>().PlaceFauna(tree, vertices[0], icosphere);
    GameObject.Find("Fauna Manager").GetComponent<FaunaManager>().PlaceFauna(tree, vertices[1], icosphere);
    GameObject.Find("Fauna Manager").GetComponent<FaunaManager>().PlaceFauna(tree, vertices[2], icosphere);

The bottom 3 lines of code call a function that should in theory place another gameobject on top of the 3 coordinates that have been calculated above, however this does not happen. The tree-placing functions work completely and have been tested so the problem is definitely with the coordinates. I printed out the coordinates and the length of the array that holds them. The array is of length 3 as it should be however the coordinates are completely off. The trees have been placed a long way away from the icosphere which means the coordinates are clearly wrong. Where have I gone wrong?

example of coordinates: -0.1, 0.0, -0.1
centre of icosphere for reference: -0.7431812, 0.4044746, -4.932096

Above you can see that the coordinates are well off

\$\endgroup\$
2
  • \$\begingroup\$ Did you remember to take into account the position/scale/rotation of the mesh object? \$\endgroup\$
    – DMGregory
    Commented Jan 27, 2019 at 18:54
  • \$\begingroup\$ @DMGregory no I didn’t, how can I edit my code to include this? \$\endgroup\$ Commented Jan 27, 2019 at 18:59

1 Answer 1

1
\$\begingroup\$

It looks like you might not be transforming the vertices by the transformation of the object in the scene.

Vector3 worldPoint = icosphere.transform.TransformPoint(localPoint);

The vertices of a mesh are stored in object-local space. So you need to transform them through to world space if you want objects under a different parent to line up.

Alternatively, you can parent your trees to the mesh object and set their localPosition instead.

\$\endgroup\$
3
  • \$\begingroup\$ What is the variable localPoint in my case? Is it vertices? Because it doesn’t like the fact that one is Vector3 and the other is Vector3[] \$\endgroup\$ Commented Jan 27, 2019 at 19:06
  • \$\begingroup\$ Then index one point, not multiple points. \$\endgroup\$
    – DMGregory
    Commented Jan 27, 2019 at 19:07
  • \$\begingroup\$ works perfectly, much appreciated \$\endgroup\$ Commented Jan 27, 2019 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .