combine children, apply mesh collider to parent

ok, I have a gameObject w/ a few different meshes as children

what I want to do is treat them as a single object so I use combine children

now I want to apply a mesh collider to the resulting parent mesh

cant figure out how to do this

the goal is to treat the parent object as a single mesh, particularly w/r/t raytracing, tracing mouseclicks etc

thanks in advance!

I am looking to do the same. Have you figured this out since you posted this 2 years ago??

Lo!

Im just tried the same, but its not work to me too.
I tried in this way:

function Start () 
	{

	gameObject.AddComponent(MeshFilter);
	gameObject.AddComponent(MeshRenderer);
	gameObject.AddComponent(Rigidbody);
	gameObject.AddComponent(MeshCollider);

	for (var child in transform)

	child.position += transform.position;
	transform.position = Vector3.zero;
	transform.rotation = Quaternion.identity;

	var meshFilters = GetComponentsInChildren(MeshFilter);
	var combine : CombineInstance[] = new CombineInstance[meshFilters.Length-1];
	var index = 0;


	for (var i = 0; i < meshFilters.Length; i++){
		if (meshFilters[i].sharedMesh == null) continue;
		combine[index].mesh = meshFilters[i].sharedMesh;
		combine[index++].transform = meshFilters[i].transform.localToWorldMatrix;
		meshFilters[i].renderer.enabled = false;
	}

	GetComponent(MeshFilter).mesh = new Mesh();
	GetComponent(MeshFilter).mesh.CombineMeshes (combine);

	GetComponent(MeshCollider).sharedMesh = GetComponent(MeshFilter).mesh;

	renderer.material = meshFilters[1].renderer.sharedMaterial;

}

This script is a mixed script, cut from several scripts, and I really cant figure out, how to make it work well.

Somebody help me, pls.

Where exactly is your script failing? Is it failing when it assigns the MeshCollider.sharedMesh, or when combining meshes in the first place?

The combining done succesfully, and it looks like the collider is fine too, at runtime it got an instance as mesh, but there is no effect: the combined mesh simply falls down through the obstacles.

I tried out a modified version(without the combining function)of this collider preparer script in a simple object. Works fine. So, as I see, somehow the combined object’s mesh not good for a collider, or I put the mesh data for the collider in a bad way.

Is it possible to combine meshes and make them static meshes with colliders generated as well?

so, any ideas? I think this is an interesting question :slight_smile:

If this is for a STATIC mesh, did you check that the “gravity” checkbox on rigidbody is unchecked?

Also there is limit of vertexes for collision MESH, for what I know.

has anyone found a solution to this? I(obviously) lose my colliders when I combine too, but still cant get the new colliders to work yet. Any solutions are welcome

See my comments at the end of this thread.
http://forum.unity3d.com/threads/71391-MeshCollider-is-not-getting-updated-when-adding-vertex-to-mesh

  1. Use CombineMeshes

  2. Example code from ScriptRef Dont do this, just leave it out
    transform.GetComponent().mesh = new Mesh();

3.MeshCollider.sharedMesh=null;
MeshCollider.sharedMesh=theNewMesh;

The mesh collider will now work and memory will not increase.

@Tzan Thanks You So Much Bro It Works. I just added These Two Lines After The Final Mesh Line!

  1. GetComponent ().sharedMesh = null;
  2. GetComponent< MeshCollider> () .sharedMesh= finalMesh;

So here is the complete Mesh Combine Script With Collider Also. You can use this script on any mesh . Thanks Me Later.:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]

public class MeshCombine : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Quaternion OldRot = transform.rotation;
Vector3 OldPos = transform.position;

transform.rotation = Quaternion.identity;
transform.position = Vector3.zero;

MeshFilter[ ] filters = GetComponentsInChildren();

Debug.Log(name + “is combining” + filters.Length + “meshes!”);

Mesh finalMesh = new Mesh ();
GetComponent ().sharedMesh = null;
GetComponent< MeshCollider> () .sharedMesh= finalMesh;

CombineInstance[ ] combiners = new CombineInstance[filters.Length];

for(int a = 0 ; a <filters.Length ; a++)
{
if(filters[a].transform == transform)
continue;

combiners[a].subMeshIndex = 0;
combiners[a].mesh = filters [a].sharedMesh;
combiners[a].transform = filters[a].transform.localToWorldMatrix;
}

finalMesh.CombineMeshes (combiners);

GetComponent ().sharedMesh = finalMesh;

transform.rotation = OldRot;
transform.position = OldPos;

for(int a = 0; a < transform.childCount; a++)

transform.GetChild(a).gameObject.SetActive(false);

}

}

I’m glad old me from 2011 was able to help a bit :slight_smile:

3 Likes