Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

combine children, apply mesh collider to parent

Discussion in 'Editor & General Support' started by ceteris, Feb 18, 2008.

  1. ceteris

    ceteris

    Joined:
    Feb 9, 2008
    Posts:
    25
    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!
     
  2. wgrand

    wgrand

    Guest

    Joined:
    Mar 11, 2010
    Posts:
    14
    I am looking to do the same. Have you figured this out since you posted this 2 years ago??
     
  3. no204

    no204

    Joined:
    May 27, 2010
    Posts:
    7
    Lo!

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

    Code (csharp):
    1. function Start ()
    2.     {
    3.  
    4.     gameObject.AddComponent(MeshFilter);
    5.     gameObject.AddComponent(MeshRenderer);
    6.     gameObject.AddComponent(Rigidbody);
    7.     gameObject.AddComponent(MeshCollider);
    8.  
    9.     for (var child in transform)
    10.  
    11.     child.position += transform.position;
    12.     transform.position = Vector3.zero;
    13.     transform.rotation = Quaternion.identity;
    14.  
    15.     var meshFilters = GetComponentsInChildren(MeshFilter);
    16.     var combine : CombineInstance[] = new CombineInstance[meshFilters.Length-1];
    17.     var index = 0;
    18.  
    19.  
    20.     for (var i = 0; i < meshFilters.Length; i++){
    21.         if (meshFilters[i].sharedMesh == null) continue;
    22.         combine[index].mesh = meshFilters[i].sharedMesh;
    23.         combine[index++].transform = meshFilters[i].transform.localToWorldMatrix;
    24.         meshFilters[i].renderer.enabled = false;
    25.     }
    26.  
    27.     GetComponent(MeshFilter).mesh = new Mesh();
    28.     GetComponent(MeshFilter).mesh.CombineMeshes (combine);
    29.  
    30.     GetComponent(MeshCollider).sharedMesh = GetComponent(MeshFilter).mesh;
    31.  
    32.     renderer.material = meshFilters[1].renderer.sharedMaterial;
    33.  
    34. }
    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.
     
  4. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    Where exactly is your script failing? Is it failing when it assigns the MeshCollider.sharedMesh, or when combining meshes in the first place?
     
  5. no204

    no204

    Joined:
    May 27, 2010
    Posts:
    7
    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.
     
  6. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,332
    Is it possible to combine meshes and make them static meshes with colliders generated as well?
     
  7. no204

    no204

    Joined:
    May 27, 2010
    Posts:
    7
    so, any ideas? I think this is an interesting question :)
     
  8. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,332
    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.
     
  9. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    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
     
  10. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
  11. og52958

    og52958

    Joined:
    Apr 3, 2020
    Posts:
    4
    @Tzan Thanks You So Much Bro It Works. I just added These Two Lines After The Final Mesh Line!

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

    og52958

    Joined:
    Apr 3, 2020
    Posts:
    4
    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<MeshFilter>();

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

    Mesh finalMesh = new Mesh ();
    GetComponent<MeshCollider> ().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<MeshFilter> ().sharedMesh = finalMesh;



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


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

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


    }

    }
     
  13. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    I'm glad old me from 2011 was able to help a bit :)
     
    acaciobiu, twicejiggled and masterton like this.