Hi everyone! I had the problem mentioned in the subject and I only found one similar thread in answers.unity. There I got a solution, but it looks weird and maybe there is a better way to solve this.Below I post an extended description and my code that’s easy to understand, but to summarize:
- If I create a quad mesh and assign it to a GO, I can get a grid with meshes in the right position but all the GOs are at zero transform.position (I lose that info for later use)
- If I try to set the GO transform.position (doesn’t matter the method used or if I do it after/before assigning the mesh) I keep the right transform.position but all the meshes are moved.
- The present solution (lines between comments in the code below) requires to recalculate an reassign again all the meshes values! I think that this looks bad and redundant
Full description and code:
I’m making a procedural grid of quads (so, it’s a plane with square tiles). I post my code below to create a single tile: I create a quad and assign it to a GO, this is simple and works fine with all the tiles in the right position but all the GOs transform.position are at the zero position. The problem is that I need to add a dynamic destroy of this objects when they are far from the player, and I don’t have a tile transform.position to compare!
I’ve added a vertices’ repositioning (as the linked thread mentioned) and now I have the correct positioning and the transform.position correct values, but it’s an ugly solution! Maybe anyone knows about a better option. Thank you!
{
Vector3[ ] vertices = new Vector3[4] { pointX0Z0, pointX1Z0, pointX0Z1, pointX1Z1 };
mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = tris;
mesh.RecalculateNormals();
mesh.uv = uv;
tempQuad = Instantiate(segmentPrefab, Vector3.zero, Quaternion.identity);
tempQuad.GetComponent<MeshFilter>().mesh = mesh;
// Required added steps to keep position in transform:
// - doing nothing = right position but transform.position zero
// - assign transform.position only = GO with right transform.position but mesh moves position
tempQuad.transform.position = pointX0Z0;
vertices[0] = pointX0Z0 - pointX0Z0;
vertices[1] = pointX1Z0 - pointX0Z0;
vertices[2] = pointX0Z1 - pointX0Z0;
vertices[3] = pointX1Z1 - pointX0Z0;
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = tris;
mesh.RecalculateNormals();
mesh.uv = uv; // this maybe is the only one not required
//
tempQuad.GetComponent<MeshCollider>().sharedMesh = mesh;
}```