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:
- the rotated plane on the right - I want to get the vertices from this plane
- 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().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;
}
}
You’re doing TransformPoint twice on the vertex position for some reason. That’s going to negate any transform changes on the transform itself.
Also use code tags in future please: Using code tags properly
1 Like
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.
// the plane to generate a grid on
public GameObject currentMeshObject;
// the mesh plane
public Mesh currentMesh;
// an array of vertices, that will contain all points on the plane in the plane's coord system
public Vector3[] localVertices;
// the plane's point transformed to the world coord system
public List<Vector3> worldVertices;
void GetWorldVertices()
{
// this will get all the mesh vertices (local coords)
// then convert them to the world coordinates
// get vertices based on the object's coordindate system
localVertices = currentMesh.vertices;
// transform them into the world coordinate system
for (int i = 0; i < localVertices.Length; i++)
{
Vector3 localV = localVertices[i];
Vector3 worldV = transform.TransformPoint(localV);
worldVertices.Add(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;
}
}
// Start is called before the first frame update
void Start()
{
currentMesh = currentMeshObject.transform.GetComponent<MeshFilter>().sharedMesh;
GetWorldVertices();
}
You should be calling TransformPoint on the transform of the currentMeshObject, not on the transform of the GameObject the script is attached to:
Vector3 worldV = currentMeshObject.transform.TransformPoint (localV);
2 Likes
Thank you both this works great!!