- Home /
Question by
magnadox1 · May 07, 2020 at 01:15 AM ·
combinemeshescombinechildren
New to unity, struggling to make objects combine into single physics object.
so my goal in the end is to have the ability to get all children objects of an empty into one object, so far i've gotten the thing to render the correct way with the correct material; but now i've been stuck for two about 16 hours on how to get their individual mesh data to add together and form one mesh collider. for the life of me i cant get it to do anything other than fall through the floor and i'm starting to loose it, here is the code i have so far: using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class MeshC : MonoBehaviour
{
public GameObject myObject; //new object
public Material outputmat;
void Start()
{
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length)
{
Debug.Log(meshFilters.Length);
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
//meshFilters[i].gameObject.SetActive(false);
i++;
}
transform.GetComponent<MeshFilter>().sharedMesh = new Mesh();
transform.GetComponent<MeshFilter>().sharedMesh.CombineMeshes(combine);
MeshCollider[] childcoll = GetComponentsInChildren<MeshCollider>();
CombineInstance[] combmesh = new CombineInstance[childcoll.Length];
int n = 0;
while (n < childcoll.Length)
{
Debug.Log(childcoll.Length);
combmesh[n].mesh = childcoll[n].sharedMesh;
combmesh[n].transform = childcoll[n].transform.localToWorldMatrix;
//combmesh[i].gameObject.SetActive(false);
n++;
}
//transform.GetComponent<MeshCollider>().mesh = new Mesh();
//transform.GetComponent<MeshCollider>().mesh.CombineMeshes(combmesh);
//
//myObject.AddComponent<MeshCollider>();
GetComponent<MeshCollider>().sharedMesh = transform.GetComponent<MeshFilter>().sharedMesh;
transform.gameObject.SetActive(true);
}
Comment