When I used the combine meshes script (Unity - Scripting API: Mesh.CombineMeshes) it moves the meshes. I’ve read to try moving to parent to 0,0,0 1st or multiply each child mesh localToWorldMatrix with the parent’s worldToLocalMatrix, but neither of those fix the issue. I’ve pasted the code below as an example. Does anyone know how to combine a bunch of child meshes without them being moved?
Vector3 tempPos = gameObject.transform.position;
gameObject.transform.position = Vector3.zero;
gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
Material mat = null;
Matrix4x4 pMat = gameObject.transform.worldToLocalMatrix;
for (int i = 0; i < meshFilters.Length; i++)
{
mat = meshFilters*.GetComponent<MeshRenderer>().material;*
combine_.mesh = meshFilters*.sharedMesh;
combine.transform = pMat * meshFilters.transform.localToWorldMatrix;_
_meshFilters.gameObject.SetActive(false);
}*
transform.GetComponent().mesh = new Mesh();
transform.GetComponent().mesh.CombineMeshes(combine);
gameObject.GetComponent().material = mat;
transform.gameObject.SetActive(true);_
gameObject.transform.position = tempPos;