2
\$\begingroup\$

I'd like to get world space coordinates for a specific mesh vertex. Transform.TransformPoint() seems to be what I need however I'm unable to get the correct position.

In the scene I have an object called "head" at Pos (0, 0, 0) Scale (0.03, 0.03, 0.03) and I'm trying to get the position of vertex id 590 in order to spawn another object at that specific location.

Mesh mesh = GameObject.Find ("head").GetComponent<MeshFilter>().mesh; Vector3[] vertices = mesh.vertices; Vector3 newPos = transform.TransformPoint (vertices [590]); Debug.Log (newPos); 

The value of newPos is not on the head object.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You're using the transform from the script's GameObject rather than the "head" GameObject.

GameObject gameObject = GameObject.Find ("head"); Mesh mesh = gameObject.GetComponent<MeshFilter>().mesh; Vector3[] vertices = mesh.vertices; Vector3 newPos = gameObject.transform.TransformPoint (vertices [590]); Debug.Log (newPos); 

Also, if your "head" object has skinned animations then just using TransformPoint will miss it.

You need to first create a snapshot of your skinned mesh using SkinnedMeshRenderer.BakeMesh to obtain the vertex position after skeleton animation, before passing it to TransformPoint.

\$\endgroup\$
2
  • \$\begingroup\$ Perfect, thank you. Using gameObject.transform and works as expected! E: and also thanks for the heads up on using skinned meshes, I might have run into that issue at one point. \$\endgroup\$ Commented Aug 7, 2017 at 11:40
  • \$\begingroup\$ @stephane - your answer is helping with my sanity. I am stuck on a similar issue where I have a world point and want to force a skinned point into that world point. But going from world to skinned local is perplexing me. I am using BakeMesh without issue, but I am unclear on how to use the baked mesh along with InverseTransformPoint. Any tips? \$\endgroup\$ Commented Apr 7, 2018 at 18:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.