x


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?

more ▼

asked Mar 25 '10 at 03:02 AM

Kith gravatar image

Kith
541 27 28 38

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first
more ▼

answered Mar 25 '10 at 09:11 AM

ZanzibarDreams gravatar image

ZanzibarDreams
273 10 12 22

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 25 '10 at 07:30 AM

KvanteTore gravatar image

KvanteTore
1.5k 9 15 33

(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Nov 29 '11 at 08:23 AM

Dentvanboo gravatar image

Dentvanboo
1

(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Mar 25 '10 at 03:05 AM

bowditch gravatar image

bowditch
1.6k 16 21 44

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, instead of the weapon he was holding previously. What's the best way to achieve that?

Mar 25 '10 at 03:20 AM Kith
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5274
x3935
x997
x563
x406

asked: Mar 25 '10 at 03:02 AM

Seen: 2326 times

Last Updated: Nov 29 '11 at 08:23 AM