You may recall last week I wasn't sure if I was going to dig into the barely functional camera controller some more, or start working on animation. I decided to skip any more work on the camera controller for now namely because, as you recall, ultimately I want this to be a giant robot game, and I'm thinking robots with turrets, and I'd want the camera attached to the rotating upper half of the mech. Additionally, we'll be using a 3rd person shooter point of view, and until we get a crosshairs on our hud, and some weapon systems, there's not a lot of point in fine tuning the camera. So let's get Ethan animated, instead of just gliding across the terrain.
Well the first thing to realize is - I didn't use Ethan. See, the Ethan prefab that is provided in the standard assets is a great resource. In fact, it's a little too good, as it has a lot of stuff preconfigured already, and that was exactly the stuff I wanted to configure myself. To learn what those components are, how they're set up, and how they related to each other.
So the first thing I did, and I did this part at periodic points throughout the week, was to start getting familiar with animation basics in Unity 5. To be precise - humanoid animation basics. And I started that by just working through Unity's own tutorials, and even reading the manual.
So as I mentioned, Ethan is a bit too far along for our purposes. I really just wanted a straight up humanoid mesh. I dug around the asset store, and found Space Robot Kyle. Kyle is from Unity tech itself, and has been around since, forever. Most importantly, he's a simple, skinned mesh, and a material. In other words, he's perfect.
Creating an Animator, and an Avatar
So I got Kyle imported easily enough, but it still took a bit of work to actually get going. I created an empty Animator controller easily enough, but I was having trouble creating an Avatar to associate with the model. I would go to the rig tab of the model view, set Avatar Generation to "Create from This Model", and click "Configure", and nothing would happen. Eventually I learned a very important lesson. And that is, if you have your Unity inspector in debug mode, about half of the automated inspector tools Unity provides for you around animation either don't show up, or don't work.
In any event, before long I had a functional, if simple, animation tree built. I added my controller script to Kyle, pointed the camera at him, and launched the game. He animated perfectly while idling.. but.. he didn't animate while moving. Oh duh. One more step - get a reference to the animator, and update the animator parameter values (in this case a whopping one - ForwardSpeed) through script. Oh hey now he works!
One tweak I noticed right away was that he didn't animate backwards. Stupid me, when I initially set up the blend, I set the thresholds from 0 to max forward movement speed. Well this doesn't account for moving backwards. I adjusted the blend tree again to account for negative speeds, and limited his backwards moving speed to be limited to half his forward moving speeds. Overall, pretty pleased, and not bad for what was about a single night's work. Here are the results.
This is the fourth segment in an ongoing series wherein I build a game from scratch in Unity3D. If you'd like to read the entire series, click the Game Development tab above, or just click here.