Daily Progress — Getting Free Animations from Mixamo into your Unity Game
If you need animations for your game characters, Adobe offers Mixamo, a huge library of various animations you can use in your game. Here, I’ll demonstrate importing an idle state for the Fireman character I chose yesterday.
After logging in, I searched for an idle animation and quickly found one I liked where the character is looking around, ready for action!
Click the download button, and be sure FBX for Unity is selected as well as selecting ‘with skin’.
Then drag your newly downloaded animation into the fireman model. Technically it doesn’t have to go there, but it’s convenient to keep things together. Then go into rig and under animation type, select Humanoid and apply.
Next, create a directory called Animations and create a new Animator Controller, rename it to Player_Animator_Controller and drag it into the models Animator.
We want to drag the idle animation into the animator, but its options are currently restricted. If we make a duplicate (ctrl-d), the options will open up to us so let’s make one and then drag it into the Animator.
Also just to keep things organized, go ahead and create an Animations folder in the character folder asset and drag the newly duplicated idle into it.
Be sure to go into the scene and select the character first, then double-click on the animator component or open the window if it’s available, and then drag the idle animation in. It should automatically attach itself as the first action. Double click the idle animation, and on the right hand side, be sure to click ‘Loop Time’, to make the animation loop around and keep playing.
Now just hit the play button and if all goes well, your character will be looking around and ready for action!
Next we wired the animations in the animator window and added a float parameter of Speed to make the transitions. idle to run, speed was greater than 0, run to idle, speed would be less than 0.
Next, we took control of the animator through code. Originally we’d be making a separate PlayerAnimation script, but for now, we would be coding things in the Player script.
First We would make a reference to our animator, while this is nothing new, the animator was a child object so we took that into account with the GetComponentInChildren method.:
Next it was just a matter of calling the controller and using the horizontal input as the parameter. Because moving left is a negative number, we’de use the Mathf.Abs command to return a positive whole number. It works very well!
Accidental deaths aside, I think it worked pretty well!
I then imported a jump exactly the same way as the first method, though the animation did cover some distance, and there was no option to keep it ‘in place’ so we just downloaded it as it was.
we were able to remove the distance with the ‘bake into pose’ option for the root transform y position after duplicating the animation.
I also tried to wire it a bit differently at first transitioning from any state into jump and using a trigger, but it didn’t seem to work as I expected. I then created transitions back and forth from jump to both idle using a bool parameter called Jump and running states and that worked a lot better. tweaking the transitions is also key to a character control that feels good. Here’s the logic I used:
Idle to Jump: no exit time, 0 transition duration,
Conditions: Speed < .2, jump = true.
Jump to Idle: no exit time, .1 transition duration
Conditions: Speed < .2, jump = false
Jump to Run: no exit time, .1 transition duration
Conditions: Speed > 0, Jump = false
Run to Jump: no exit time, 0 transition duration
Conditions: Speed > 0, Jump = true
Idle to Run: no exit time, 0 transition duration
Conditions: Speed > .15
Run to Idle: no exit time, .25 transition duration
Conditions: Speed <.1
While a little more work than using a trigger the code was still simple, we created a new bool variable called _jumping and in the player script, we just added the appropriate _anim.SetBool(“Jump”,true) lines.
And since we set jumping to true, we’de need to set it to false. When the player is grounded is the most logical place.
This resulted in a really good working jump!
Finally there was the issue of the player not facing the right direction. The solution was to rotate the player 180 degrees depending on the value of horizontalInput!
while the logic of the first line looks a bit strange, it’s really an if else statement in the form of a ternary operator! Basically you give an if statement, and then a question mark. After the question mark, if it’s true, it’s the first item before the colon, and if it’s false, then it’s the option after.
so here: facing.y = _direction.z >0 ? 0:180;
remember that direction z holds the horizontal input, and if the horizontal input is moving right, then it’s positive 1 and therefore true, and if it’s moving left, it’s -1 and therefore false. So if it’s true, rotation is 0, and if it’s false, rotation is 180 degrees or flipped.
I hope it makes sense. But there you have it! Everything I learned today! Tomorrow we’ll be going into ledge grabbing!