Hi guys, I’m having trouble using properly a meshcollider on a prefab. I may have miss a basic setting, I’m quite new to Unity. I have found threads about the same problem, butt with no response.
Here is my problem:
I am creating a prefab with different meshes, LOD purpose. The prefab itself has a meshcollider. Each time I change the LOD, I would like to change my prefab meshcollider two. I did it, but it doesn’t work like it should (or at least as I would like):
The mesh seems to be placed in the wrong way, but it is not. the object’s pivot point is:
So I have made test and it appears that the problem is the transform: rotation and scaling actually, despite the fact that I never change the prefab transform during the creation process. If I add an other gameobject to my prefab, apply to it the same transform has other gameobjects and had the meshcollider to this newly created gameobject, the meshcollider is correct:
(I know I shouldn’t use such heavy meshes for meshcolliders, but this is just a test.)
I want to have the meshcollider settled properly, but in the prefab itself, and not in one of its child. is there a way to do this?
And I also have an other question : is this possible to define multiple boxes in a single boxcollider?
Edit: We can use multiple boxColliders, more details on the the Box Collider documentation page.
I am not 100% clear on how you are doing things, but I can say a few things about this that might clear things up. Your prefab’s Transform, Mesh (aka geometry), and Mesh Collider are three separate components. While changing the Transform will affect the Mesh and Collider, the reverse is not true. Also, changes to the Mesh will not affect the Collider unless you specifically tell Unity to do so.
Seeing the Transform (what you’ve called the “pivot point”) a distance away from your mesh can happen one of two ways:
You’ve manipulated the mesh in your scripts. Remember that changing the mesh does not change the Transform.
When the model was built, it was not placed correctly relative to (0,0,0)
Running into this issue really shouldn’t cause any real problems unless your scripts depend on the Transform being located at a certain point on/in/near your mesh.
On to the collider. When you add the MeshCollider component to an object, Unity will use the existing mesh to create the actual collider. If you subsequently modify the mesh, the collider is not automatically updated. From my somewhat limited experimenting, the only sure-fire way I’ve found to update a MeshCollider correctly is to destroy it (using Component.DestroyImmediate), manipulate the mesh, and then add a new MeshCollider. There really should be a more efficient way to do this, but I haven’t been able to find one that is reliable.
Anyway, I hope that this answers your questions. Let me know if I wasn’t clear on anything.
It’s the second case you describe, but I already knew that. I was just explaining why the collision mesh appears under the rendered meshes. I agree that I didn’t made it very clear.
Thanks for the information, I’m gonna try to manipulate the CollisionMesh transform itself, to see if it can solve my problem.
I have figured out that the transform of a component is the same as its GameObject’s one, so I can’t do what I wanted to do first in this case…
I have read that activating or deactivating MeshCollider is a really heavy operation, so I have ran into tests. The involved frames can be ten time longer than the originals ones. So this is out of question too to use a MeshCollider for each LOD.
The only option that seems to be satisfying is the thing I would like to avoid at first: adding a new GameObject in the prefab, and using this GameObject only for the MeshCollider.