I’ve got an issue with using combined meshes with a MeshCollider. I’ve got a script that takes multiple meshes from mesh filters in child objects, combines them and then passes them to a Mesh Collider. The issue is that the resulting mesh ends up basically not existing. Nothing collides with the expected result in-game and the collision mesh itself doesn’t show up in the editor while the game is paused.
I’m thinking it might be a timing issue, since I also have a script that goes through and activates/deactivates a number of objects based on certain set parameters. However I’d like a second opinion on that.
Oh, and my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class combineCollisionMeshes : MonoBehaviour
{
void Start()
{
MeshFilter[] meshColliders = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshColliders.Length];
for (int i = 0; i < meshColliders.Length; i++)
{
Debug.Log(meshColliders.Length);
combine[i].mesh = meshColliders[i].sharedMesh;
combine[i].transform = meshColliders[i].transform.localToWorldMatrix;
Debug.Log(combine.Length);
}
Mesh collisionData = new Mesh();
collisionData.CombineMeshes(combine);
collisionData.Optimize();
transform.GetComponent<MeshCollider>().sharedMesh = collisionData;
transform.gameObject.SetActive(true);
}
}
It’s a bit of a variant on the mesh.CombineMeshes example code, with a setup that allows it to further manipulate the resulting combined mesh before passing it to. I had read in another, much older thread that optimizing the combined mesh will sometimes fix the problem and decided to try it. Obviously it didn’t in this case.
The reason I’m doing this is because I have two separate collision meshes that are causing “trip” collisions on a passing rigidbody object where they overlap (seriously, Unity’s collision system needs some serious tweaking. When SM64, a game from 1997, handles collisions better than your engine you know you have a problem) and I couldn’t find a way to fix that with just the existing meshes.