Swapping out parts of a model in code

Okay, I don't even know where to start with this one, so I have no code to present. But basically I want to be able to import a model (A guy with a gun), and be able to switch out the actual gun object with another object, and retain the animation. Basically the guns (modeled in Maya) would be similar in shape and proportion, but how would you do that in code? Is that possible?

One way is to combine the meshes of the character with the mesh of the gun whenever the character chooses to change the weapon. At runtime you use the Mesh.CombineMeshes method to glue the meshes together.

Take a look at the character customization example, and specifically the CharacterGenerator script which puts the pieces together.

I am not really sure of the performance implications of this approach. Compared to having a separate GameObject for the weapon, it lowers the number of SkinnedMeshRenderers, which could improve performance. On the other hand, it might take a moment to actually combine the meshes.

Take a look at this question, maybe can help http://answers.unity3d.com/questions/6366/how-can-i-change-sword/6375#6375

You should be able to split the animations up in the FBX importer in Unity without touching code for the import.

Create animations for both and then call them when you want the swaps. In the Animation Split feature in FBX Importer, make enter the start and stop keyframes for each set of animations.

Is that helpful, or am I misinterpreting what you are trying to accomplish?

Yeah I’m trying to do this exact thing as well. I have my character model, and a same character model with a gun, and when I run to a pickup with this gun I want the model without the gun to swap to a model with a gun… How easy does that sound? :smiley: Any tips?