Search Unity

Modular armors and odd challanges

Discussion in 'Scripting' started by Foestar, Apr 23, 2020.

  1. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    Hey all, so my current project is pushing me to learn how to do armor and helm changes on my character model. I've never done this sort of thing before but felt like I was up to the challenge!

    At the moment I have a single character model which serves as the base. I then added in a armor mesh, and a helmet mesh. Both are separate objects within the same 3DS Max file. I then rigged all 3 objects, (Skin, Armor, Helm) to a biped with excellent results. I took the same base and did this for 3 more sets of armor and helmets with the intention of switching them out via script at runtime. This is where the challenges first started coming in.

    I made a color picker setup and allowed the player to change their characters skin color which works fine. But as I switched the skins I found some very odd results. Before continuing I would like to note that before I made all these armor and helm models I tested my theory using a simple model of him naked and another with armor and it worked perfect. But with my new armors and helms only 3 work as intended, the other 1 does not. It does the same thing as the armors, where it skews all crazy!

    So at this point I thought I'd do some reviewing of my objects individually. So when dragging and dropping my models into the editor and reviewing each helmet I noticed that 3 (all the working ones) had the same root bone on that mesh where as the one that didn't had a different root bone. If I changed the rootbone to the same as the others then applied those others to that one it seemed to work perfectly fine. It was at this point that I realized my issue may have been with bones. Though I'm not sure why only that one was different as all helmets were rigged exactly the same. So not sure why it imported differently.

    The armors however were a much bigger odd story as none of them have worked thus far regardless of what I've done. I will include a video showing my current work and my issues in depth below.
     
    Last edited: Apr 23, 2020
  2. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    Okay, so with a few tweaks to the code I have managed to get the armors to switch properly mesh wise, but still having some issues. So let's sum this up. The helmet changes work because all the helmets are rigged exactly the same way. Simply to the head as it's own mesh with each model.

    The armors on the other hand are all rigged differently, meaning some have arm bands so they include the arms with rigging, others leg bands so they include legs, etc. This means we can't just change them because they don't share the same rigging, bone weights, root bones, etc. Or at least that's what I'm gathering so far. This is what causes the mesh to explode or implode into a mess.

    So what would be a good solution? Well I thought why not just keep things simple like we did with our helmet code. Our helmets were switched out easy peasy like so:
    Code (CSharp):
    1. newGoblinHelm.sharedMesh = goblinHelms[1].sharedMesh;
    All the above is doing if simply changing the private SkinnedMeshRenderer newGoblinArmor (which is our players model) into the armor that I store in an array of SkinnedMeshRenderers within the same script. I then get the materials of said mesh the same way.
    Code (CSharp):
    1. newGoblinHelm.sharedMaterial = goblinHelms[1].sharedMaterial;
    This works %100 perfectly! My helmets change easy peasy lemon squeezy no problem! And it's because they have the same rig setup.

    Now onto the armor! I do the same thing and...... Oh my what have I made!!?!?!?!???? It's okay, we knew this was gonna happen because of the rigging setup. Let's add some stuff. We get the root bone, the boneweights and finally maybe even the bones themselves so we know which one's we are using?
    Code (CSharp):
    1. newGoblinArmor.rootBone = goblinArmors[1].rootBone;
    2.             newGoblinArmor.bones = goblinArmors[1].bones;
    3.             newGoblinArmor.sharedMesh.boneWeights = goblinArmors[1].sharedMesh.boneWeights;
    And all of a sudden I can change my armor regardless of their setup!!! Wait, somethings off. The armor isn't where it's supposed to be AND it's no longer bending with the body like it should.....

    After a few minutes of investigating I find that this is because we are in fact getting the bones alright. And I mean the bones themselves in the other mesh. So our current models bones aren't even applied to the model which is why it may not line up or move. So what now? I gotta find a way to attach it to the bones like it did, but in the correct newGoblinArmor bones equivelent instead. I think if I can figure out how to do that I'll have it. But hot damn is this a mind buster. :eek:o_O

    Gonna keep on with it. If anyone else has any input on this I welcome it. I have looked into stitching and combining meshes and even the poor cheat which I refuse to do where I just have multiple meshes enable and disable but change skins and helm on each. I would very much like to get this working without excessive code and clutter. So i'll keep at it.
     
  3. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    Bwahahahahahahahahaaha! I finally got it! I found out how to properly switch meshes despite having different rig setups without having to stitch together stuff or long coding!!!!! It was such a frustrating learning experience but so satisfying once I had it!

    So the issue is when switching meshes the meshes must have similar bone setup otherwise they don't work. It gets all skewed, implodes, explodes, etc. And you can't just grab the correct bones from the prefab of the armor. Doing this would make the mesh properly skin, but not at the right location or to the biped we are using either because we are grabbing those bones specifically from that other skeleton which wont work for this one.

    This made me think about how I go about getting the right bones from the proper skeleton. Then it clicked! I need a list of the bones in the armor required, then check through the bones of my current rig and fetch them into a list. I can then apply this list to my current mesh after it's been changed to the one I want.

    So now my meshes and armors are fully changeable despite their bones with only like 20 lines of code. Or about 40 if you include the reference array for each armor, helm, and skin color.
     
    eisenpony likes this.