- Home /
Generating A Custom Collider For Multiple Meshes - Script
Hi :) This might be quite hard for me to explain, so bare with me.
First I will try to explain what I have got going on in my project. I have a ship parent called 'Player' which has a ridigbody on it and can be controlled by a constant force. The player can customize the ship by adding square modules to it, which have the prefix 'Mod_'. These modules are in a group called 'Modules' that is a child of the 'Player', so that they can be controlled by the constant force as well.
This picture is after the ship has been customized, notice the individual modules:
I wish for these modules to have colliders on them, so the ship can bump into things etc. I have tried putting box colliders on each individual module. This works in terms of colliding with things. But it seems to interfere with the ridigbody, and how the ship moves around. It makes it much slower and more unpredictable when more than 1 collider is applied to the ship. I think this is because the colliders on each modules are hitting each other, effecting the movement of the ship. I have tried making it so the modules cannot collide with each other on the collision matrix, but that made no difference.
Its seems the only solution would be to have 1 collider for the whole ship, that works out what the overall mesh looks like and generates one around it.
Is this possible to do? As I know you can use mesh colliders for a single mesh, but can you do it so it generates one to go around multiple meshes within a parent?
Also if you have any other ideas on a solution that would be great!
Thanks for your time reading this hefty passage of text. If you are struggling to understand what I am talking about, just say and I will try to explain further :P
Answer by roojerry · Jul 31, 2013 at 02:32 PM
I would assume you would be able to CombineMeshes and add a mesh collider to the resulting mesh.
Aah-ha, I was just looking at that now. Combine$$anonymous$$eshes sounds perfecto :P I'll try to give it a go
Unity has a perfect 'Combine Children' script that comes with it, and that combines the meshes like I wanted.
However that didn't fix my original problem with the colliders effecting the movement ridigbody.
I found setting the inertiaTensor manually did, eg: rigidbody.inertiaTensor = Vector3(0.5, 0.5, 0.5);
Answer by ragnaros100 · Jul 31, 2013 at 02:29 PM
I know you can create a custom mesh and use the information from the 3d model to generate a collider with the mesh collider component. But that'll only work for static models that cant get customized... I'm no code wizard so I don't know how you can procedurally generate colliders
Yeah that what I realised as aswell. I wonder if there is a way to merge all the individual modules into a single mesh, so that a collider can be generated based of that.
Sounds like something that could be possible :P