Transformation of vertex coordinates into world space not working

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:

        GameObject bridgeStart;
        bridgeStart = GameObject.Find("cliff3"); //cliff3 is a gameobject in the scene
        Mesh bridgeStartMesh = bridgeStart.GetComponent<MeshFilter>().mesh;
        Vector3[] meshVerticesStartLocal = bridgeStartMesh.vertices;
        Vector3[] meshVerticesStart = new Vector3[meshVerticesStartLocal.Length];
     
   for (int i = 0; i < meshVerticesStartLocal.Length; i++)
        {
           meshVerticesStart[i] = transform.TransformPoint(meshVerticesStartLocal[i]);
        }
       
        Debug.Log("Lokaler Vertex:" + meshVerticesStartLocal[5]);
        Debug.Log("Globaler Vertex:" + meshVerticesStart[5]);

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

        Vector3[] points = new Vector3[kNumPoints];
        for (int pointNum = 0; pointNum < kNumPoints; pointNum++)
        {
            points[pointNum] = Vector3.right * pointNum;
        }
        Debug.Log(points[5]);
        Vector3[] transformedPoints = new Vector3[kNumPoints];
        transform.TransformPoints(points, transformedPoints);
        Debug.Log(transformedPoints[5]);

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.

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? :wink:

2 Likes

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.

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).

https://discussions.unity.com/t/945755/8

1 Like

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”.

1 Like

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

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.

1 Like

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. :slight_smile:

Thanks a lot it seems like exactly that was the issue.