For this week's blog post, I'm going to get a bit more technical than we usually do in these blog posts. This is very Unity3D specific, so if that's not your bag then you've been warned.
The characters in Shibe Warz can have any combination of armor, boots, helmets, and one weapon/spell in each hand, so how do we attach those models to the character and have them animate when the character does?
I'll start with the helmet and weapon models, because the method for those is very generic and can be useful in other games. I've uploaded the entire .cs file here if you want to look at it before/while you read.
1. Create prefabs of the models to be used.
This is pretty straightforward, drag in all the assets you need and make the weapon and armor prefabs.
2. Create a script to assemble the finished model
I named my script "UnitBuilder.cs", and it has public GameObject objects for each model that we just made a prefab for. In Shibe Warz we have cloth, leather, and plate armor, as well as a bunch of weapons, so the top of my script looks like so:
...etc. The prefabs made earlier can now be dragged into this script, so they are easily changeable later on. Note the "baseModel" object, which is the prefab of the completely naked unit, which we will be building on top of.
3. Instantiate the unit's naked model
This is pretty straightforward. Notice that I'm using Network.Instantiate here because Shibe Warz is multiplayer over a network. If it's a single player game, you'd just use Instantiate.
4. Attach the helmet
This is where things get a little tricky. The premise behind having models "attach" to the character is to make them children of the original model, because a child's position and rotation, once initially set, are locked in with respect to the parent. So let's say you make two cubes, A and B. You make A the parent and B the child. You shrink B and put it inside of A. Now when you move A, B will move with it, retaining its position inside of A at all times.
So, what we're going to do is make the helmet a child of the unitGameObject created above. But we have to be careful here, because the unit's model will be animating, and as such the head will be moving around a lot. So what we really want to do is make the helmet a child of the unit's head, that way no matter how much it moves around, the helmet will move with it. How do we do that? Well, first we have to find the head in the model hierarchy. This is how it looks in the unity hierarchy view:
So now that we know where the head is, we can find it in code and place the helmet there. Beware, the following is hard-coded, which is never good. But it was getting late, so I was just trying to get things working. Please refer to my previous blog post, where I talk about how messy your code will likely get while developing a game.
One thing you may recognize is the use of the helmPosition object. I discovered that I couldn't simply put the helmet at position (0,0,0) because then it wouldn't be fit on the head properly, so I had to mess with it a little bit to make it look good. Once it looked good, I simple copied the x, y, z values and stored them as private variables in this class.
And voila! The helmet is now instantiated, placed on the head, and made to stay there by setting transform.parent = charHead.
5. Attach the Weapons
This is almost identical to attaching the helmet, the only difference is instead of attaching it to the unit's head, you attach it to their hand. For the sake of brevity I'm omitting the code, but I've uploaded the entire file here if you would like to see how the sausage was made.
6. Attach the armor and boots
I saved this part for last, because I'm not really proud of how it was done, and I know in my heart of hearts there's a better way to do it, but it works, and if it ain't broke don't fix it, right?
When Anthony created the armor, he was generous enough to attach it all to the same skeleton as the base unit model, and animate it in the exact same way. This made my job extremely easy, as all I had to do was instantiate the armor at the exact same spot as the unit, and any time an animation is run I just animate both the unit's model and the armor's model. The same was done for the boots.
So! This was pretty much the entire process of building a dynamic model from scratch. I hope this can be useful to someone, and if anyone has any questions or wants to say, "Hey that was a stupid way to do that, you should do it this way..." and then provide some awesome way to do it, feel free to leave a comment here or hit us up on twitter, @verusgames. After all, the purpose of this post is to help people in the future with the same problem!