How to animate and move a simple player with Mixamo and Unity/C#

Let’s see how to use free resources from Adobe Mixamo and Unity’s Animators to animate a basic player character!

🚀 Find all of my Unity tutorials on Github!

This article is also available on Medium.

This tutorial is available either in video format or in text format – see below 🙂

Get the scripts and assets for this tutorial on my Github 🙂

A quick overview

In this episode, we’re going to program a very simple player controller that can be idle, run around left and right or jump, “side-scroller” style.

To do this, we will use free animations from Adobe Mixamo, and Unity Animators and Rigidbodies – our movement will therefore be based on physics.

Are you ready? Then, let’s dive in!

What is Mixamo?

The first step is to get some animations from Adobe Mixamo.

Mixamo is an online platform where you can download rigged characters, poses and animations for free! So even if you don’t have your own 3D model, you can easily download any animation with one of the built-in avatars, like this red one I’ll use here. The lib is pretty comprehensive – it has a lot of various actions and movements, with a focus on video games projects. Just browse the list and you’ll find animations of a biped standing, walking, running, jumping or attacking.

Note: from what I’ve seen, it doesn’t have animals and quadrupeds yet…

In our case, we want to make a basic hero using this red X-Bot model.

So, first, to be able to download resources from the website, make sure you are logged in with either a Mixamo account or other usual providers like Google. Once you are signed in, you’ll get access to all the resources and you’ll be able to browse or directly search for something.

Here, I’ll go ahead and download three animations: an idle stance, a run animation and finally a jump animation.

Because all models are rigged with the same standard, you can apply any animation on your current model – you don’t need to stick with the ones that show this specific model in the preview!

Also, after you’ve picked an animation, you can click and drag the 3D view on the right to orbit around the model, and you can zoom in and out.

Some animations also move the root of the model because they imply some larger movements. If you don’t want this overall translation, you can check the “In Place” box on the right to constrain the animation to only the non-root bones.

So – for each of these animations, I can simply click the “Download” button and keep the default export options. I want to use Mixamo’s avatar in Unity, which means I have to make sure I do export the animation “with the skin”. You can also select various FBX formats if you want, but the default is fine for Unity.

Ok: now, I have 3 FBX files on my computer. To import them in my Unity project, I can just drag them anywhere in the “Assets” folder or a subdirectory.

And then, we can easily drag any of our three assets in an empty scene and we directly get our nice X-Bot! 🙂

Updating the import settings

But we need to setup some import settings for these FBX files so that Unity can properly extract all the required data from them because, at the moment, it’s not possible to play any of our animations.

When you select one of the Mixamo imported asset, you’ll see that the Inspector opens a special import window with four tabs: “Model”, “Rig”, “Animation” and “Materials”. This is the FBX importer window.

Model

The first tab is for choosing specific subparts to exclude from the import, or to convert between various basis by changing the axes or the units.

Here, we only need to change one thing, that is to bake the axes, because Mixamo models are designed with game engines like Unity in mind, so they’re configured to match most of the defaults.

Rig

In the second tab, we need to change the import parameters to create an avatar for our model. So, in the “Avatar Definition” dropdown, select the “Create From This Model” option.

The idea is that in Unity, the animation of a humanoid character relies on the definition of an avatar: this asset is used to match the bones in your character’s skeleton with the animation keys and curves. As explained in the docs, you need to “reconcile the bone structure of the Model to its Animation“, and so you basically need to do a re-mapping of the bones stored in your FBX file with in a Unity humanoid avatar.

Important note: once you’ve changed the settings, don’t forget to hit “Apply” at the bottom to actually keep those settings! 😉

Animation

In the third tab, we can preview the animation on our model and check its settings. The import is fine overall, although we can rename the clip to give it a more meaningful name and we need to make sure that the “Idle” and “Run” animations loop by checking the “Loop Time” option.

Again, remember to apply the settings at the bottom!

Materials

Finally, the “Materials” tab lets us choose how we want the materials to be assigned on our model. For now, they are automatically created and loaded from the FBX file: you can find them inside your imported asset, as sub-assets. But if you want, you can also replace them with your own Unity materials. Here, I’ll just stick with the default config.

Tip: re-using our avatar

Don’t forget to apply the same settings on your two other Mixamo assets! For the other rigs, however, we don’t need to create new avatars; instead, we can simply copy the first one we created by passing in the avatar sub-asset in this slot.

Configuring the Animator with an Animator Controller

Ok, at that point, our X-Bot is ready to be scripted and animated! So, first, let’s prepare all the different animations it will use, along with some useful parameters, thanks to Unity’s Animator component.

This component allows Unity to apply a specific animation to your model, using its avatar. And if you take a look at your instantiated Mixamo asset, you can see one was automatically added to it. That happened when we updated our “Rig” config and asked to create an avatar from this model.

But if we want to control which animation the Animator uses, we need to create another asset: an Animator Controller.

Basically, this component lets you arrange and link various animations together using a state machine. Each Animation Clip is referenced by Unity as a state, and you can then design sort of a flow-chart with the different Animation Transition from one clip to another.

To link the new Animator Controller to the Animator, just drag it into the dedicated spot in the Inspector.

Now, we need to open the Animator window to inspect this Animator Controller more closely. To do this, just go to the menu bar, then Window, then Animation and Animator.

And so here, we can see the state machine of how our basic model. For now, it’s empty, because we haven’t added any animation to it:

Adding our “Idle” state as default

To add states, or in other words animations, we simply have to open the Mixamo imported assets and click and drag the animation clip that are inside each to our Animator window.

Let’s try and add our “Idle” animation first. As soon as you drop it into the window, it gets added to the state machine and linked to the entry point. It is shown in orange, which means it will be the default state.

So if you try and play your scene, you see that the X-Bot switches to this animation and plays it again and again (remember we set it to loop earlier!). If you want, you can even take this Animator window down and examine the state machine while it runs. The blue progress bar indicates the state that is currently playing and we see it just loops infinitely for now.

Adding the “Run” and “Jump” states

To add the other states, just open the other assets and drag the animation clips to the Animator window. You see that this directly creates new blocks (i.e. new states in our state machine) with the associated animation.

Those states are shown in gray which means they are not default states.

Creating parameters, triggers and transitions

And so the next step is to create transitions between all of these states so that our avatar can actually use the other animations – don’t forget that, for now, it is stuck in its initial “Idle” state!

To create a transition, all you need to do is right-click a block and then click on the target state. This will create an arrow between the two that you can select to further configure the transition conditions and settings.

First of, a really cool thing with Unity’s Animator is that it can blend between the animations to make smoother transitions. This is controlled via the transition time; by tweaking this value, you see that the animations overlap more or less and that you control the time it takes for the model to go from one pose to the other.

Then, the other important part of this panel is the conditions settings, at the bottom.

Transitions can be of two types: either conditional or unconditional.

An unconditional transition will simply wait for a given time, that we call the “Exit Time”, before switching to its target state. This is typically very useful when you want some animation to play once, and then as soon as it’s finished your state machine should transition to another state.

A conditional transition, on the other hand, uses one or more user-defined parameters or triggers to switch to the target state at specific moments. So rather than saying “wait for 2 seconds”, you can say “wait for this parameter to be true”, or “wait for this trigger to be activated”.

And, of course, those parameters and triggers can be accessed and set via C# scripting, as we’ll see very soon! But these parameters and triggers needs to be created first, and this happens here, in the Animator window. On the left, you see that you have a “Parameters” tab where you can add new variables that are global to the state machine and that can be of four types: floats, bools, ints or triggers.

In our case, we want to create two parameters: one will be a boolean called “Running” and the other a trigger called “Jump”. You see that boolean parameters have a little check next to them, so you can decide that your parameter is initially true if you want by setting its value here. Triggers just have a little button you can click to call them.

Now, we want to use these variables in transitions!

First let’s finish this transition between the “Idle” and “Run” states. We want it to happen when “Running” becomes true, so we can add a condition on this parameter and check for the “true” value. Since we have a condition, the Exit Time is now optional, and we should actually reset it to 0 so that the transition starts as soon as the “Running” parameter is set to “true”. We can leave a little bit of blend, with something like 0.1 second of transition time.

Of course, we also need to create the reverse transition: it’s configured exactly the same except that we check for the “false” value for the “Running” parameter in our condition.

If we play the scene again, this time, we can manually go and check the “Running” parameter to force the transitions. As soon as we click the checkbox, the transition occurs and the state changes. Our X-Bot is now running! If we uncheck the parameter and set it back to false, we get our second transition and we go back to the “Idle” state.

For the jump state, we also want two transitions from and to the “Idle” state, but this time the first one will use our “Jump” trigger, so we don’t need to check for any value, and the second transition will be unconditional. We’ll simply wait for the jump animation to complete and go back to our “idle” state afterwards.

Once again, we can enter play mode and click the “Jump” trigger to force the transition. Then, as soon as we’ve waited the right amount of time, the state machine automatically goes back to our “Idle” state.

A nice trick is that we can easily allow the transition to the “Jump” state if we are in the “Run” state, too, just by using the “Any State” block that’s automatically created by Unity. This way, no matter which state we’re currently in, if the “Jump” trigger is called then we’ll go to the “jump” state.

However, we still need to transition back to a specific state, so I’ll arbitrarily keep the “Idle” state.

Controlling our Animator from our C# scripts

We now have a nice and clean Animator Controller that works really well!

The only thing is that we obviously don’t want to manually click the checkboxes and triggers – instead, we want these parameters to be updated in our scripts along with the position of the character, depending on the player’s inputs.

Important note: in this tutorial, I will focus on handling keyboard inputs, but of course you can easily extend this to other kinds of inputs, especially you’re a bit familiar with Unity’s new input system 😉

Anyway – to handle this logic, let’s create a C# script called PlayerController.cs, and put it on our avatar game object. Then, double-click the script to open it.

Preparing the script

The first thing we need to do is create a reference to our Animator. This will allow us to set the “Running” parameter and call the “Jump” trigger we just defined. Let’s also create a reference to a Rigidbody, because remember I want to use the basic physics system of Unity to handle my player movements.

While I’m at it, I’ll tell Unity that both those components are required: this way, I’m sure that any game object with my PlayerController script also has an Animator and a Rigidbody.

If I go back to my Unity editor, I see that the missing Rigidbody was automatically added on my game object. I also need a BoxCollider, though, so that my object collides with the ground and doesn’t just fall. Make sure that it is properly sized to match the avatar dimensions!

All of this means that, in my Start() function, I can use the GetComponent() built-in to fill both my references:

Now, since I’m using Unity physics, I’m going to use the FixedUpdate() update for my logic. Usually, it’s better to implement things in the FixedUpdate() when working with physics because it gives you deterministic and properly computed values:

Making our player run left and right

The first thing I’ll code is my run feature. To do this, we’ll need another private variable, a little _running boolean flag that is initially false, and that will record whether I’m already in the running state or not. I’ll also need something to store the current value of the horizontal input axis. Since in this video, I only want my character to go left or right, I’ll just look at this axis.

It’s defined by default in my project settings, in the Input Manager tab, and assigned the arrow or the <a> and <d> keys:

So, what I want to do is get the current value of this input, which goes from 0 (if no key is pressed) to -1 if the left key is pressed, or to 1 if the right key is pressed.

If the value is not null, then I need to toggle on the running. I won’t check for an exact zero match, though, because there can be some approximation with floats so it’s better to check with a small threshold, like this:

In that case, I have to move my character thanks to its Rigidbody, and optionally to transition to the “Run” animation state if my Animator is not already using it.

For the movement, because I’m in this “side-scroller” setting, I can simply set my rotation to look either left or right, and then move along my new forward vector with a given speed.

This speed value is of course arbitrary and you can tweak it to your liking – I just find that 3 is nice for my set up!

Note: the minus signs appear because of the axes of the imported model; you might need to adapt this depending on your 3D model, if it doesn’t move consistently with the inputs 😉

For the Animator, I want to check if I’m already running; in that case, I don’t need to do anything. Else, I’ll set my _running flag to true for next time, and also set the “Running” parameter in my Animator component:

If I let go of the keys and the horizontal input goes back to 0, then if I was running I should reset to the “Idle” state; meaning that I need to set my _running flag back to false, as well as the parameter in the Animator:

All of these setters will automatically call the right transition, like before when we unchecked it manually.

If I run my scene, I can now move left and right: the position of my player updates and it switches between the “Idle” and “Run” states!

Adding the jump

The second feature is the jump and it’s actually pretty easy to add 🙂

Back in our FixedUpdate(), at the very top, we can check for our Rigidbody velocity. If it’s somewhat zero (again, with a little threshold to be sure we don’t have issues because of approximations), and if we’re pressing the “Jump” input, then we’ll call our “Jump” trigger in the animator and add some force to the Rigidbody:

The jump force is another magic number, here I found that 6 was nice but it does take into account the mass of my Rigidbody, because I’m using the “impulse” force mode, so if you decide to change the mass of your Rigidbody, you’ll have to change this value to match!

A little improvement of the inputs

A final tweak we can make is back in our inputs definition, for the horizontal axis, we should put the gravity parameter to something way larger, like 1000. This way, the input will be back to zero as soon as we release the key, rather than decreasing gradually over time.

Our final result

And here we are! If we run the little scene again, we can now run left and right, stand or jump from any of other two states 🙂

Of course, you might need to do some adjustments on the start and end frames of your animations, or on their speed. This can be changed by going either to the importer window, and setting the clip start and end frames, or by going to the matching state in your Animator and setting the speed value.

Here is finally a little demo of our player controller:

Conclusion

That’s it for today, folks 🙂

In this tutorial, we saw how we can use some free resources from Adobe Mixamo to quickly set up a player character in our Unity scene with some basic movements and animations. We then used Rigidbodies to move around along an axis and to jump.

I hope you enjoyed this quick Unity tutorial and the dual video/text versions. Feel free to react in the comments and tell me if you like this format — and as usual, go ahead and share your ideas for future topics you’d like me to make Unity tutorials on!

Leave a Reply

Your email address will not be published.