Monday, August 24, 2015

Unity3D At Home Project - Day 2 - Simple Character Controller

So the  Standard Assets pack comes with a 3D character controller right out of the bag. And it's fairly functional - does everything you want it to do.  In fact, it's actually a little more complex than I want for first pass.  Their 3rd Person character controller contains separate components for the Controller, an Input Controller, a Camera Controller, and an Animation component, and contains code to handle interactions between all of those.

I'm trying to put pieces together piece by piece, so what I really wanted, to start with, was just a very simple character controller. Renaissance Coders YouTube channel came to the rescue, with a great, nofrills character controller you can code up in literally about 20 minutes.  You can find their tutorial here.

This allowed me to get a simple, functional character controller up in the afternoon, and I completely understand all the pieces. I also added a capsule collider to my avatar mesh (Ethan), so he would collide with the trees as I moved him around.  Once I added the capsule collider and set it's offset appropriately, I could turn on gravity on the RigidBody.  

One other interesting thing I learned here was that you need to freeze rotation on the rigid body in all three axis. The first time I tried to move poor Ethan, he fell flat on his face, and then I could only move him up and down. We have zero tie-to-ground code here, so if the unit runs up against the tree, for instance, it only slowly moves back down to the ground, based on gravity.

This controller is super simple, and there's a billion lines of code to write here for it to become an game-ready controller, but it fulfills our purpose of being able to move the unit around.

Here's a video: