# Question Transformation of vertex coordinates into world space not working

Discussion in 'Scripting' started by Blatherkite, May 11, 2024.

1. ### Blatherkite

Joined:
Nov 1, 2022
Posts:
3
Hello, I wish to write a script which reads the vertex information of two meshes and creates a new mesh based on the location of said vertexes. To do this think I need to transform all the vertices into the wold space since otherwise the locationinformation is based on the position of the gameobject. To do this I wrote this code:
Code (csharp):
1.
2.         GameObject bridgeStart;
3.         bridgeStart = GameObject.Find("cliff3"); //cliff3 is a gameobject in the scene
4.         Mesh bridgeStartMesh = bridgeStart.GetComponent<MeshFilter>().mesh;
5.         Vector3[] meshVerticesStartLocal = bridgeStartMesh.vertices;
6.         Vector3[] meshVerticesStart = new Vector3[meshVerticesStartLocal.Length];
7.
8.    for (int i = 0; i < meshVerticesStartLocal.Length; i++)
9.         {
10.            meshVerticesStart[i] = transform.TransformPoint(meshVerticesStartLocal[i]);
11.         }
12.
13.         Debug.Log("Lokaler Vertex:" + meshVerticesStartLocal[5]);
14.         Debug.Log("Globaler Vertex:" + meshVerticesStart[5]);
15.
The problem at the moment is that the TransformPoint function doesn't seem to be doing anything. The debug messages show that the vertex coordinates do not change at all.
Debug Log Messages:
Lokaler Vertex: (-0.50, 0.50, -0.50)
Globaler Vertex: (-0.50, 0.50, -0.50)

I also created another script based on a tutorial just to see if I can get the function working at all
Code (csharp):
1.
2.         Vector3[] points = new Vector3[kNumPoints];
3.         for (int pointNum = 0; pointNum < kNumPoints; pointNum++)
4.         {
5.             points[pointNum] = Vector3.right * pointNum;
6.         }
7.         Debug.Log(points[5]);
8.         Vector3[] transformedPoints = new Vector3[kNumPoints];
9.         transform.TransformPoints(points, transformedPoints);
10.         Debug.Log(transformedPoints[5]);
11.
Here the function worked as it should so I assume that the problem with my original script is that I am pulling the positions from vertex data but I don't know why that even causes issues and how I would go around fixing it since they are both Vector3 arrays. (I also tried using transform.TransformPoints without any success). Thank you in advance.

2. ### CodeSmile

Joined:
Apr 10, 2014
Posts:
6,943
The result you get would be expected if the gameobject this script runs on is positioned at the origin (0,0,0). Is that the case?

You probably intended to use bridgeStart.transform.TransformPoint(s) in the case the script isn't a component of "cliff3" which seems likely, since you had to "find" it first.

Btw, you know what everyone says about GameObject.Find?

Kurt-Dekker and Bunny83 like this.
3. ### Blatherkite

Joined:
Nov 1, 2022
Posts:
3
Thank you for responding I am unfortunatly still a newbie so I do not know what everyone says about GameObject.Find. I checked online on how to retrieve a mesh from a object in the scene and this was the solution that I found. Is there a better way to get access to mesh data?
"cliff3" is not in the origin.
The transform information of "cliff3" are as follows:
Position (X/Y/Z): (-10/0.7899999/-9.08)
Rotation (X/Y/Z): (10/0/0)
Scale (X/Y/Z): (1/1/1)
It is a cube with 24 Vertices and 12 Triangles. I used the cube prefab from Unity and placed it into the scene. The intent is for the script to build a bridge between two gameobjects during runtime so the script is attached to a empty gameobject.

4. ### Ryiah

Joined:
Oct 11, 2012
Posts:
21,697
It's extremely slow and it only becomes worse the more objects in the scene and the further down the Hierarchy the target object is. In my tests I've seen it anywhere from a few times slower (dozens of objects) to hundreds of times slower (thousands of objects).

CodeSmile likes this.
5. ### Bunny83

Joined:
Oct 18, 2010
Posts:
4,205
Don't get distracted from your actual question and the answer that CodeSmile has given you. Specifically
In your last post you even mentioned the "transform information" of your "cliff3" gameobject. Of course when you want to transform from the local space of the cliff3 gameobject into world space, you have to use the Transform component of that very gameobject and not just a Transform of some other gameobject. Currently you implicitly use
``this.transform``
which means you use the Transform component of the gameobject where this script is attached to.

Think of this analogy: imagine you own several houses and each house has it's own property somewhere in town. The properties can be seen as a gameobject and lets say the door of your houses is the origin / reference point of each house. So you could specify the location of each house in the town based on where the door is located. This is your transform information. Your vertices are other things inside the house. So you specify for example that your desk in house "A" is 5m away from the door. When you want to specify where the desk is in relation to the town, you would need to use the houses position (maybe the street and house number) and relative to that you add those 5m. However when you are currently in house "C", it makes not much sense to look for the desk at a location 5m away from the door of house "C" since the desk is in house "A".

spiney199 likes this.
6. ### CodeSmile

Joined:
Apr 10, 2014
Posts:
6,943
GameObject.Find - why not to and how to else:
https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html
https://stackoverflow.com/questions/49715785/better-way-to-find-gameobjects-without-gameobject-find
https://medium.com/@akb1ggs/unity-devs-stop-using-gameobject-find-fe986046bc9a

It's not even so much about performance than it is about its potential for causing all sorts of "random" issues. Rename an object in a scene: breaks. Make a typo in either the object name or the string: breaks. Have two objects in the scene of the same name: breaks. It may even change on occassion which object it happens to find first.

The connection of the name of an object in the scene with some string in code is not obvious - which objects can you safely rename and which you can't? Given enough uses, you cannot know. It affects your behaviour: you stop renaming things in the scene, ever. Your "cliff3" now actually represents a Tower? Not unheard of.

It's important to discuss these things because the more we spread this info the less likely new Unity users are to fall into this trap.

ArachnidAnimal likes this.
7. ### Bunny83

Joined:
Oct 18, 2010
Posts:
4,205
Note that with:
I didn't mean to play down the points you have risen. To the contrary. However the OPs response was only addressing those points which are fair points, but not related to the problem at hand. Yes, Find "should" be avoided when possible, but it's also not a huge issue. Things like that almost always have to be learned the hard way. So it's fine to mention them and trying to somewhat guide new users a bit but in the end it's something they have to decide on their own.

For example reflection should be avoided whenever possible as it's also slow, breaks common OOP contracts, is error prone and can also fail with runtime errors at unexpected changes. Though yet almost every serialization library uses reflection under the hood to do its job. The right tool for the job is not always as clear cut.

Since this question was more about struggling with the concept of different coordinate spaces and different Transform components we shouldn't focus too much on side topics. Otherwise people would just overwhelm the reader with suggestions like "always use git", "don't use JsonUtilty", "save often", "use a proper IDE", ... which all are valid points but not related to the issue.

8. ### Blatherkite

Joined:
Nov 1, 2022
Posts:
3
Thanks a lot it seems like exactly that was the issue.