0
\$\begingroup\$

I've imported a number of track-terrain pieces into a game I'm working on, and I would like to get the mesh vertex arrays so I can position objects at their exact end. However, when I try to collect the arrays, they're off by a huge amount. A vertex located at the start of the object is 0,-0,-0.01 and a vertex located at the end of the object is.. 1, 0, 0.1. This doesn't seem to make any sense.

These are also -after- the coordinates are transformed into world space.

For a mesh that is 16 meters long and comprised of 70 points, I would expect vertex 65 to be at 3,0,16.

This is the code I am using to collect the information.

CurrentObject = Instantiate(TerrainObjects[0]) as Transform;
meshFilter = CurrentObject.GetComponent<MeshFilter>();
vertices = meshFilter.mesh.vertices;
CurrentObject.transform.parent = gameObject.transform;
//Vector3 EndVectorLocation = transform.InverseTransformDirection(vertices[10]);

Debug.Log(transform.TransformPoint(vertices[10]));

Have I missed something in getting the correct transform positions?

\$\endgroup\$
  • 1
    \$\begingroup\$ "These are also -after- the coordinates are transformed into world space." Your code doesn't seem to have any code to do that. meshFilter.mesh.vertices gives you the raw mesh vertices without any transformation applied. Note that Unity automatically applies a rotation to the transform of models imported from blender files to fix the different coordinate systems. \$\endgroup\$ – Philipp Aug 29 '16 at 14:16
  • \$\begingroup\$ //Vector3 EndVectorLocation = transform.InverseTransformDirection(vertices[10]); <-- It's commented out, but it is in there to show that I've tried it. How would I apply the correct sizing and rotation to the vertex array in question? \$\endgroup\$ – Merlin Aug 29 '16 at 14:23
  • \$\begingroup\$ Attempting both still returns (0.0, -0.2, 0.0) in Debug.Log. It almost feels like it's acting on a 0-1 scale. ---- Vector3 EndVectorLocation = transform.TransformVector(vertices[67]); \$\endgroup\$ – Merlin Aug 29 '16 at 14:34
0
\$\begingroup\$

You might want to look at the documentation of transform.InverseTransformDirection:

Transforms a direction from world space to local space.

You want to do the opposite.

You should use Transform.InverseTransformPoint if the vector represents a position in space rather than a direction.

In your case it does represent a position in space.

So the method you should be using is transform.TransformPoint.

However, I would not recommend you to give specific meaning to specific vertices of your 3d models. Blender does not guarantee that these numbers stay constant. So when you make the slightest modification to your models, it might break your game until you figured out the new index of the "magic vertex".

A better solution is to turn all the imported models into prefabs. Then add child game-object to them which you position at the position you want to have a special meaning. For example, when you have road segments which are straights and curves, you would place a "Connection Anchor" in the middle of both ends and then spawn the next one so their "Connection Anchors" align.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ I was afraid of that. The meshes are already prefab and stay static - the only thing that changes is position when the generator runs through. It's basically just terrain track. My thought by using vertex positions was that it might make it easier to position the exact end point of the meshes. The good thing about this solution is it does allow more varied track. \$\endgroup\$ – Merlin Aug 29 '16 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.