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
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.
3Duaun
January 23, 2011, 4:41pm
9
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
Tzan
January 23, 2011, 6:10pm
10
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
Use CombineMeshes
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.
og52958
September 18, 2020, 1:58am
11
@Tzan Thanks You So Much Bro It Works. I just added These Two Lines After The Final Mesh Line!
GetComponent ().sharedMesh = null;
GetComponent< MeshCollider> () .sharedMesh= finalMesh;
og52958
September 18, 2020, 1:59am
12
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);
}
}
Tzan
October 4, 2020, 2:58pm
13
I’m glad old me from 2011 was able to help a bit
3 Likes