The 3 Cs Toolkit in Unity (2023)

Make your character come to life with Mecanim, Cinemachine, and Input System.
Published March 2nd, 2023
Jason Tu
Note: this blog post was presented as a talk at Unity NYC's March 2023 meetup.

Thomas Was Alone

Thomas Was Alone is a fantastic game.

It's a prime example of what you can achieve as a programmer — even if you have limited art skills.

However, there might come a point in your game dev journey when you no longer want to make boxes, snakes, and geometric shapes.

Instead, you want to create full-blown 3D characters that come to life with animation:



  • Link, doing his spinny sword thing.
  • Mario, jumping and somersaulting around in Super Mario 64. Or...
  • Samus, running and strafing and morphing into a ball and firing all sorts of different projectiles.

Perhaps even an undead hero like in Dark Souls, where you could parry a skeleton's sword swing, and stab them in the back.

But how do you make that jump: from abstract shapes, to beautifully-animated characters in a 3D space?


Whereas 3D characters required a deep and expansive skillset in the past, these days you can get by with the help of some powerful tools.

Here, I'll show you!

In the rest of this blog post, I'll give you an overview of 3 modern Unity packages:

  • Mecanim: for character animation,
  • Cinemachine: for your game's camera, and
  • Input System: for your controls.

I'll teach you their key mental models, and walk you through my simple character creation workflow that employs all 3 packages.

By the end of this post, you'll come away with a firm grasp of the character implementation process, and be empowered to create living and breathing characters of your own in Unity.

Let's get started. 👇

Agenda

  1. 🏭 Consider your animation workflow.
  2. 💰 What to do if you can't model or animate.
  3. 🎥 How to think like a cinematographer.
  4. 🎮 Make your game controls rebindable.
  5. 👿 On escaping Unity tutorial hell.

A little about myself.

I'm a Senior Full-Stack Engineer with a decade of experience in tech.

But in January 2023, I decided to go on a sabbatical to build my professional portfolio as a Gameplay Programmer.

These days, I’m creating what I like to call Game Codebase Tours, where I build open source games in Unity, and teach you to do the same through polished and well-documented pull requests.

The first one I’m building is Baguettorio — a factory builder game about an engineer who crash-lands on an alien planet, and must create a giant baguette to escape the planet.

It's completely free; it's completely open source. I have no agenda beyond demonstrating my ability and sharing what I learn.

If you want to follow the project’s progress, you can watch the repo above. Or sign up to be notified when I have significant updates to share:

🏭 1. Consider your animation workflow.

3D character

Photo by Anshita Nair on Unsplash

Behind every beautifully-animated character is a pipeline: a series of tasks that produce your character asset.

It's actually fairly involved:

Modeling, Texturing, Rigging, Skinning, Import into Unity, Animation, C# Integration

And whether you're a solo developer or a small team, you must decide how you'll tackle each step of the process with your team's skillset.

Consider the countless workflow options that you can explore:

TaskOptions
Modeling
  • Make your own 3D models
  • Buy 3D models
  • Have an AI do it 😅
Texturing
  • Make and UV map your own textures
  • Buy pre-textured models
  • Have an AI do it 😂 (I'll stop now)
Rigging
  • Create your own character rigs
  • Buy pre-rigged models
Skinning
  • Assign your own weights that define how your 3D model deforms under rig movement
  • Buy pre-skinned models
Animation
  • Ragdolls. Inverse kinematics. DOTween. Author animations in Unity. Unity Timeline. Unity Physics. Buy animations. Blend shapes. Procedural animation. Motion capture. Rotoscoping.

Within this space of options, you might compare options by evaluating monetary cost versus required skill:

cost versus skill2

You might evaluate expressiveness, or whether you can achieve your desired creative result.

For example, if you wanted to create a climbing animation, would you be able to do so with purchased animations alone?

You might evaluate short-term versus long-term benefit. That is, do you want to reuse this workflow in future games? If so, is it worth spending more time learning the skillset now, versus later?

Lastly (and most importantly), you might evaluate scope, or how long it takes to perform your selected workflow, and whether that scope is acceptable for your time and budget.

For example, procedural animation is insanely cool. But you need to be good at math. And doesn't traditional animation give you (more or less) the same result?

Ultimately, your workflow decision depends on all the factors above.

💰 2. What to do if you can't model or animate.

Given the considerations above, I knew I couldn't create 3D models and animations myself. At least not within a reasonable timeframe.

So I shelled out $80 for polyperfect's Low Poly Ultimate Pack on the Unity Asset Store:

polyperfect

For animation, I chose to use Mixamo: an online service that provides a set of free humanoid animations.

It's as easy as dragging your purchased 3D model into Mixamo's web application, selecting an animation, then downloading and importing that animation file into your Unity project:

running character

polyperfect provides a helpful YouTube video to do this.

Then, using a blend tree in Mecanim (Unity's animation state machine and visual scripting language), I blended between idle, walk, and run animations based on a single MovementSpeed parameter:

state machine

astronaut mecanim

And with very little time investment, I had a working and animated character controller:



There remain a few details to polish up. For example, the character looks like it's moonwalking, since a walk cycle doesn't correspond to an equivalent amount of traveled distance.

(Or maybe that can be explained away since the character is an astronaut.)

But this is a fix-able problem. And I can use Unity's Animation Rigging package to add animation detail later, such as pointing the player's head at targets of interest, or positioning the character's hands.

If you had to choose a cheap starter workflow for 3D character creation, I'd highly recommend this one.


To study the changes that implemented this initial character controller, please see the GitHub pull request.

🎥 3. How to think like a cinematographer.

Once you have a character walking and running around, the next glaring problem is the camera.

A camera fixed to one place won't do. So what can you do?

For simple games, you could code your own camera script.

But sooner or later, you might need far more dynamic camera behavior: the ability to bounce back and forth between locations, follow the player, zoom in and out, and follow pre-determined paths.

Observe the following clip from Ocarina of Time, and notice the richness of its camera behavior:



Luckily for us, we can achieve similar results with far less effort, through the power of Unity's Cinemachine package.

And by grasping its underlying concepts, you can lean on the shoulders of giants and customize your game's camera to your heart's content.

🧠 Mental models

The core metaphor for Cinemachine is that you're operating a film camera. So the entire package is couched in the language of cinematography.

Fundamentally, there are 2 core components:

  1. The Cinemachine Brain (attached to your Main Camera), which takes control and overwrites your camera's values:

    cinemachine brain

  2. Various "virtual" cameras, that serve as waypoints for the Main Camera to transition between. For my character controller's sandbox, I have a single virtual camera fixed by a rig behind the player's shoulder:

    cinemachine virtual camera

The Cinemachine documentation sums up the workflow:

  1. Create a Cinemachine Virtual Camera.
  2. Choose a GameObject to follow.
  3. Choose a GameObject to look at. (It may be the same as the followed object.)
  4. Then select camera algorithms for following and looking.

I ended up implementing a similar approach to this tutorial from Unity, where an invisible camera arm (or "rig") is stuck to the player's shoulder:



  1. Player Shoulder Target. I designated the player's shoulder (a separate GameObject from the player) as my virtual camera's target.
  2. Movement. I wrote a script to control the player's movement relative to the Player Shoulder Target's orientation.
  3. Shoulder rotation. I also wrote a script to rotate the shoulder target, which will indirectly rotate the camera rig.

This implementation gives a similar feel to that of Super Mario Sunshine, in that your left hand solely controls movement, and your right hand solely controls camera rotation.

For a third-person character controller, delegating camera handling to Cinemachine makes it far easier to add complex movement without having to script the behavior yourself.


To study the changes that implemented Cinemachine camera handling, please see the GitHub pull request.

🎮 4. Make your game controls rebindable.

xbox

Finally, we come to the last C in our toolkit: Controls.

In the past, one may have used the Input Manager and Input class to receive input in their game.

But doing so has one primary drawback:

If you wanted to switch between control schemes (such as Keyboard/Mouse and Gamepad), you'd need to modify your input handling. For example, you might need to define two axes for movement in the Input Manager:

Input.GetAxis("HorizontalKeyboard");
Input.GetAxis("HorizontalGamepad");

And then handle both cases whereever you process input.

Furthermore, there's zero flexibility if you want to support "rebinding" controls when playing your game. That is, the player must use your exact pre-defined device, with your exact pre-defined controls.

So how would we support rebindable controls without having to change source code?

Enter Unity's new Input System

Unity's new Input System package solves this exact problem. To use it, all you need is to keep the conceptual model behind the system's Action Editor in mind:

inputs

  1. Actions. The player verbs of your game. Instead of relying on hard-coded inputs, actions serve as a layer of indirection to hide your inputs behind.
  2. Bindings. The device inputs that you want to tie to actions.
  3. Action Maps. Groupings of actions. For example, you might have one set of actions for player movement, and another for driving.
  4. Control Schemes. Groups of required devices (such as keyboard and mouse), and their corresponding bindings.
  5. Action Properties. Configuration for each game action so that you get the desired input value.

Using the Input System becomes as easy as reading values:

playerInputActions.Player.Move.ReadValue<Vector2>().normalized;
playerInputActions.Player.LookAround.ReadValue<Vector2>();
playerInputActions.Player.Run.IsPressed();

Or responding to events:

private void Awake()
{
    playerInputActions.Player.Pause.performed += Pause_performed;
}

private void Pause_performed(InputAction.CallbackContext context)
{
    Debug.Log("Pause button was pressed.");
}

Where the primary difference is that you are now coding against game actions instead of specific input axes or buttons. In many ways, it is the Adapter pattern for device inputs.

With the Input System implemented and input bindings added, you can easily connect a wireless Xbox controller (over Bluetooth) to your Unity editor, and see your game played with an actual controller:

controller2

And controlling your character with a controller makes it that much more real.


To study the changes that implemented the new Input System in an actual Unity project, please see this GitHub pull request.

👿 5. On escaping Unity tutorial hell.

In the book Ultralearning, author Scott Young defines the Directness principle of learning anything deeply:

"Go straight ahead. Learn by doing the thing you want to become good at. Don't trade it off for other tasks, just because those are more convenient or comfortable."

So what does that mean for us? It means making characters.

You now have all the tools to do so. To recap:

  1. Understand tradeoffs. Consider the cost, skill, expressiveness, short-term benefit, long-term benefit, and scope of your selected animation workflow.
  2. Use asset packs. A great way to get started with character creation is through a batteries-included asset pack combined with Mixamo’s set of humanoid animations.
  3. Lean on Cinemachine. You can create dynamic cameras with little effort, through the power of a CinemachineBrain that blends between multiple Cinemachine Virtual Cameras.
  4. Abstract your game inputs. Lean on the “Action” abstraction provided by the Input System package to easily add controller support to your game.

Armed with this overview of the process, I hope that you're now well-equipped (and inspired) to make dynamic, living 3D characters of your own.

If you make something cool, let me know! Just drop a reply in the comments below. 🙂

Did you like this post?

Sign up to be notified when I publish something new: