- Home /
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?
Answer by ZanzibarDreams · Mar 25, 2010 at 09:11 AM
Take a look at this question, maybe can help http://answers.unity3d.com/questions/6366/how-can-i-change-sword/6375#6375
Answer by KvanteTore · Mar 25, 2010 at 07:30 AM
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.
Answer by bowditch · Mar 25, 2010 at 03:05 AM
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?
I'm not trying to split the animations up. I know how to set up the "idle" animation frames, "walk" animation frames, etc. What I'm trying to accomplish is...Well let me put it this way lol. The end result is that I want to be able to pick up a new weapon, and when I do, have my player model hold that weapon, ins$$anonymous$$d of the weapon he was holding previously. What's the best way to achieve that?
Answer by Dentvanboo · Nov 29, 2011 at 08:23 AM
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? :D Any tips?
Your answer
Follow this Question
Related Questions
Can I make animations snap to a frame? 1 Answer
Maya rigged object not importing correctly! 5 Answers
maya animation not importing correctly 0 Answers
no matter what i do i can't import an animation from maya 0 Answers
Maya to Unity Animation problems 0 Answers