- Home /
How do I get vertex positions of the referenced game object, and not its mesh?
OK, I'm probably missing some terminology but here's what I'm doing:
I've got a script that references a game object which has a mesh. I'm getting the vertex positions of that mesh.
What I'm getting (the problem):
Unity seems to be referencing the actual mesh, but not the mesh manipulated by the game object.
I'm using this vertex array to as positions to emit particles.
Question:
How do I get the vertex positions of the mesh of the game object?
Thanks for reading :^)
Answer by Eric5h5 · May 08, 2014 at 04:38 AM
My usual technique:
var thisMatrix = transform.localToWorldMatrix;
// Get world space for the first vertex in the mesh
var realPosition = thisMatrix.MultiplyPoint3x4(meshPoints[0]);
(You would of course loop through the entire array; the code is just an explanation.)
Thank you.
I've yet to read up on the function you're using, but just using your technique I'm now getting the correct scale of the mesh, but the position is still off.
What is also peculiar is that I'm getting the exact same result as with the solution advised by robertbu below.
edit: actually not peculiar, according to the scripting reference:
$$anonymous$$atrix4x4 localToWorld$$anonymous$$atrix; Description $$anonymous$$atrix that transforms a point from local space into world space (Read Only).
If you're not familiar with using matrices for coordinate transformations, use Transform.TransformPoint ins$$anonymous$$d.
Answer by robertbu · May 08, 2014 at 04:37 AM
The vertices in the Mesh.vertices are local coordinates. To convert these vertices to world coordinates, use transform.TransformPoint() on each vertex.
Thank you for responding. I don't want to be redundant, but my answer seems to be the same for both you and Eric5h5, so I made my reply there.
I cannot think of a reason why your position would be off. I've converted mesh vertices to world positions a number of times without issues. $$anonymous$$aybe start by testing your script in a new scene on a simple cube. It also might be something you are doing in your code, so you could add your code to the question.
edit: figured it out — cause of the shifting was Shuriken emitting in local space ins$$anonymous$$d of world.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144520im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why vertex positions appear (0.0, 0.0, 0.0) ? 1 Answer
Creating a mesh for an overview map 0 Answers
Mesh.vertices are all 0? 1 Answer
Glue GameObject to vertex; 1 Answer