Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Mesh vertices appear in a wrong position

Discussion in 'Editor & General Support' started by yuvalkahlon, May 7, 2024.

  1. yuvalkahlon

    yuvalkahlon

    Joined:
    Aug 25, 2022
    Posts:
    3
    Hi all,

    I hope someone has a solution to this strange issue. I am trying to get the coordinates of a mesh plane which is rotated in space and scaled a little. What I get is the coordinates of this plane BEFORE it was rotated or scaled (original position of the plane).

    The attached images shows:
    1. the rotated plane on the right - I want to get the vertices from this plane
    2. a collection of capsules which I generated to visualize the vertices I get - these appear in the original place and size of the plane. It seems like the transformations made to the plane were ignored.

    The plane is a primitive in Unity. I tried to import a different kind of mesh as FBX, but got the same result.

    The code I'm using is below. Any idea why this happens?

    ---

    void GetWorldVertices()
    {
    // Get the mesh from the gameobject
    currentMesh = currentMeshObject.transform.GetComponent<MeshFilter>().sharedMesh;

    // get the vertices in local coordinates

    localVertices = currentMesh.vertices;

    // transform them into the world coord system
    for (int i = 0; i < localVertices.Length; i++)
    {
    Vector3 localV = localVertices;
    Vector3 worldV = transform.TransformPoint(localV);
    worldVertices.Add(transform.TransformPoint(worldV));
    }

    // draw capsules to visualize the position of the vertices
    // in world coords

    foreach(Vector3 v in worldVertices) {
    GameObject g = GameObject.CreatePrimitive(PrimitiveType.Capsule);
    g.transform.position = v;
    }
    }

    EXAMPLE RESULT.png
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,390
    yuvalkahlon likes this.
  3. yuvalkahlon

    yuvalkahlon

    Joined:
    Aug 25, 2022
    Posts:
    3
    Thanks for replying so quickly. Using TransformPoint twice indeed did not make much sense, I fixed that but still the result is the same. I'll add the revised code with code tags here. Please let me know if you can find any other reason.

    Code (CSharp):
    1.     // the plane to generate a grid on
    2.     public GameObject currentMeshObject;
    3.  
    4.     // the mesh plane
    5.     public Mesh currentMesh;
    6.  
    7.     // an array of vertices, that will contain all points on the plane in the plane's coord system
    8.     public Vector3[] localVertices;
    9.  
    10.     // the plane's point transformed to the world coord system
    11.     public List<Vector3> worldVertices;
    12.  
    13.     void GetWorldVertices()
    14.     {
    15.         // this will get all the mesh vertices (local coords)
    16.         // then convert them to the world coordinates
    17.  
    18.         // get vertices based on the object's coordindate system
    19.         localVertices = currentMesh.vertices;
    20.  
    21.         // transform them into the world coordinate system
    22.         for (int i = 0; i < localVertices.Length; i++)
    23.  
    24.         {
    25.             Vector3 localV = localVertices[i];
    26.             Vector3 worldV = transform.TransformPoint(localV);
    27.             worldVertices.Add(worldV);
    28.         }
    29.  
    30.         // draw capsules to visualize the position of the vertices
    31.         // in world coords
    32.         foreach(Vector3 v in worldVertices) {
    33.             GameObject g = GameObject.CreatePrimitive(PrimitiveType.Capsule);
    34.             g.transform.position = v;
    35.         }
    36.     }
    37.  
    38.  
    39.     // Start is called before the first frame update
    40.     void Start()
    41.     {
    42.         currentMesh = currentMeshObject.transform.GetComponent<MeshFilter>().sharedMesh;
    43.         GetWorldVertices();
    44.     }
     
  4. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    You should be calling TransformPoint on the transform of the currentMeshObject, not on the transform of the GameObject the script is attached to:

    Code (csharp):
    1.  
    2. Vector3 worldV = currentMeshObject.transform.TransformPoint (localV);
    3.  
     
    yuvalkahlon and spiney199 like this.
  5. yuvalkahlon

    yuvalkahlon

    Joined:
    Aug 25, 2022
    Posts:
    3
    Thank you both this works great!!