Player Animation
In this tutorial, you will use character models and animations from Mixamo while enabling the animations of the player character (PC) to responds to keyboard input.
Mixamo
Creating 3D characters and animating them are both complex tasks which requires a great deal of time even for experience 3D artists. Mixamo provides quality 3D characters and a huge variety of animations. Additionally, Mixamo provides you the ability to add animations to 3D characters that are not on Mixamo as well.
Go to mixamo.com, create an account, and login. Store your login credentials for future use.
Click the Characters tab and select a character you would like to use.
Click on the Animations tab and try out some animations on your character.
For now, you do not want an animation on your character. Click the [x] next to the name the animation currently applied to your character to remove it. With no animation applied, your character should be in the T pose.
Download the character with the Format set to FBX for Unity and the Pose set to T-Pose.
Within the Characters folder of your project, create a new folder with an appropriate name to save the fbx file to. For the character that we downloaded, name the folder
Granny.
In Unity, the folder and file you created should now be accessible in the Project Window. Drag the fbx object into your scene near the FPSController. Name the character in the hierarchy.
If needed, scale your character to match your FPSController and other scene elements. Notice the textures did not download.
Download the character again, this time with the Format of Collada (.dae) and the Pose still set to T-pose. Save the zip file to the same location you had saved the fbx file.
When you extract the files from the zip folder, you will have a model and a textures folder. Take the textures folder and make sure it is in the same folder as the fbx file. Then, return to Unity, and the textures will appear on the character automatically.
Return to Mixamo. With the same character selected, click back on the Animations tab select an animation you would like the character to have. You can search for a specific animation if you would like.
With the animation on the character, Download again to the same location with the settings of Format: FBX for Unity; Skin: With Skin; Frames per Second: 30; and Keyframe Reduction: none. In the Project Window of Unity, you should now have an animation for your character.
If needed, move the slider at bottom right of Project Window to far left to change to list view. Click the arrow next to the animation to expand it. Then, select the motion clip for the animation and preview it in the Inspector.
Create an AnimatorController in your character's folder and name it appropriately. For example, an AnimatorController for the example downloaded character could be named
GrannyAnimatorController.
Select the character from the hierarchy and add an "Animator" component. Drag the Animator Controller you created from the Project window into the Controller field of the Animator component.
Double-click on the Animator Controller to open it in the "Animator" window. Create an Empty State and name it appropriately. For example, the state for a Robot Hip Hop Dance animation might be named
HipHopDance.
Drag the motion clip from the project folder to the "Motion" field of the newly created animation state. When you play the game, your character should be animated. However, notice the animation only plays once instead of repeating.
To make the animation repeat, the motion clip needs to be set to loop. With the motion clip selected, click Edit button from the Inspector Window; under the Animation tab, check the "Loop Time" checkbox; and then click the "Apply" button. Play the game again to ensure the animation is looping.
Content
Player Idle
Mixamo animations can be used on non-mixamo characters. Do this to animate the player.
The Elf that was added to the scene in the previous lesson will be used as the player character, so he will need to be positioned in front of the FPSController's camera.
Move the Elf in the Hierarchy to be child of the FPSController. Then reset the position of the Elf to 0, 0, 0 so he will be at the same location as the camera.
Move the Elf on the Y axis until he is back on the floor. Zoom in for accurate placement. Set the Y value of his Rotation to 0 so he is facing in the same direction as the camera. Clicking on the FPSController should show that the Elf is in the collider of the FPSController and facing in the same direction as the camera.
Select the FirstPersonCharacter. This is the camera so you should see a camera preview when it is selected, assuming the Camera component is expanded in the Inspector. Move and rotate the so it has an over the should view of the Elf.
Place the Game window side by side with the Scene window. With the FPSController selected, press Play.
If you move the mouse around you should see in the Scene view that the camera swivels around the player as needed. However, as soon as you begin to move the camera jumps back to its normal position, which is now inside of the head of the Elf.
Why does the camera move by itself when the player moves? Well, the player moving is a big hint. On the FPSController, you will find a FirstPersonController script. This is the script with all of the code that allows the player to move based on keyboard inputs.
You may have noticed that when you walk in the game, the camera bobs up and down to give the player a feeling that they are walking.
Check off the Use Head Bob parameter and play the game again. Now the camera should remain behind the Elf where you positioned it even as you move.
[[[ VIDEO ]]] - move around, look around, and jump
Without the bobbing effect, it may feel like you are sliding around. However, the walk animation that you add to the Elf will have its own bobbing motion.
Looking in the Elf folder, there does not appear to be any animations. However, there is a Prefab for the Elf in the Prefab folder. Recall this is what you used to place the Elf in the scene.
Go back into the Elf folder and then into the Mesh folder. Here is the actual FBX character model. This is the model that you will apply mixamo animations to.
Go back into the Elf folder and add an Animations folder.
Return to Mixamo. If your current character has an animation, remove it. Then click the UPLOAD CHARACTER button. Drag in the FBX file for the Elf to upload it.
After the upload is complete, you should see the elf with a sample animation. The textures are missing, but that is ok because they are still in your Unity project.
Mixamo added a skeleton rig to the character which you can show and hide by toggling the Skeleton View icon.
Press the Next button to complete the upload. The Elf should appear as your current character. Now you can apply any of the Mixamo animations to the Elf.
If the Elf has an animation applied, remove it. Download the Elf with the Format set to FBX for Unity and the Pose set to T-pose. This model is different from the uploaded model because of the skeleton that Mixamo applied to it. Save the file in the Mesh folder replacing the original version.
Now, the "Elf_Mesh.fbx" file will be able to use Animations from Mixamo.
In the Animations tab, search for "idle". Apply the idle animation
Download the Elf with the Format set to FBX for Unity and the Pose set to T-pose. With the idle animation on the character, Download to the same Animations folder your created for the Elf with the settings of Format: FBX for Unity; Skin: With Skin; Frames per Second: 30; and Keyframe Reduction: none.
Back to Unity. Select the Idle motion clip and press the Edit button. In the Animation tab, check the Loop Time box. Then, click on the Rig tab and change the Animation Type to Humanoid. Press the Apply button to apply the changes.
Create an Animator Controller in the Elf folder, name it
Elf Animator Controller, and use it to set the Controller property of the Animator component that is on the Elf. Also, uncheck the Apply Root Motion property of the Animator if it is checked.
Open the Animator Controller, create an Empty State named "Idle", and add the Idle motion clip to the Motion field. The Elf should be in the idle state when the game is now played.
Player Controls
Mixamo animations can be used on non-mixamo characters. Do this to animate the player.
Now that you know the process, return to Mixamo to download all the animations your player will need. Search for a "walk" animation. Apply the walk cycle you want for the Elf and check the In Place checkbox, if available, so the Elf stays in one place while the animation plays.
Download the walk animation, saving to the Animations folder.
Repeat these steps to download each of the following animations:
- idle (already done)
- walk (already done)
- walk backwards
- walk strafe left
- walk strafe right
- run
- run backwards
- run strafe left
- run strafe right
- jump
- t-pose
For each downloaded animation, edit the motion clip so they are set to loop and are of type humanoid.
In the Elf Animator Controller, create a state for each of the animations. You can do this quickly by dragging each animation from the Project window into the Animator window. You can still rename the states as you wish. Place the states in some logical manner.
Create parameters of type Bool named A-down, S-down, W-down, D-down, Shift-down, and Space-down. The values of these values will be sent by the script that is detecting keyboard input and used to determine which animations should play.
Locate the PlayerAnimationBehavior script in the Scripts folder. Attach the script to the Elf.
With the Animator window visible, play the game. Check that each of the Bool values toggles on when the associated keys are pressed.
Add the transitions and conditions to trigger the correct animations depending on which keys are being held down.
Set the conditions for each transition with the following logic:
Transition | Conditions |
---|---|
Idle->Walking | W-down == True |
Walking->Running | W-down == True Shift-down == True |
Running->Walking | Shift-down == False |
Walking->Idle | W-down == False |
Idle->Walk Strafe Right | D-down == True |
Walk Strafe Right->Run Strafe Right | D-down == True Shift-down == True |
Run Strafe Right->Walk Strafe Left | Shift-down == False |
Walk Strafe Left->Idle | D-down == False |
Idle->Walk Strafe Left | A-down == True |
Walk Strafe Left->Run Strafe Left | A-down == True Shift-down == True |
Run Strafe Right->Walk Strafe Left | Shift-down == False |
Walk Strafe Left->Idle | A-down == False |
Idle->Walking Backward | S-down == True |
Walking Backward->Running Backward | S-down == True Shift-down == True |
Running Backward->Walking Backward | Shift-down == False |
Walking Backward->Idle | S-down == False |
Any State->Jumping | Space-down == True |
Jumping->Idle | Space-down == False |
The player should be able to jump from any state. Instead of making transitions from each state to the Jump, make a transition from "Any State" to Jump.
Only the transition out of the jump animation should have the Has Exit Time checked. All other animations should be able to be interrupted at any time.
Play the game with the Animator, Game and Scene windows visible. In the Scene window you should be able to see your character take on different animations from a distance. The Animator window should help you detect any errors in the setup.
Third Person Controls
Animations can be applied to the player regardless of what camera view is being used. Here you will use a new character with Unity's ThirdPersonController.
Disable the FPS Controller in the Hierarchy.Locate the Prefab for the ThirdPersonController in the Project window under Assets > Standard Assets > Characters > ThirdPersonCharacter > Prefabs. Then drag the prefab into the Scene.
Add a Camera to the Scene, add the "MainCamera" tag, and position the camera looking downward on the player. Adding the tag is important because the scripts that come with the ThirdPersonController access the camera through this tag.
If you play the game now, you should be able to control the character that is included with the ThirdPersonController. His name is Ethan. However, Ethan can run out of view of the camera. That's not good.
Attach the follow script from the Scripts folder to the Camera. Then drag the ThirdPersonController to set the script's Object To Follow property.
Play the game again and the camera should follow Ethan has he moves about the scene.
Ethan is pretty cool, but he's just not going to do as the player character. Locate the Character_Elf prefab in Assets > Characters > Elf > Prefab.
Drag the prefab of the Elf to the Hierarchy and drop it onto the ThirdPersonController. This will make the Elf a child of the ThirdPersonController.
Rename the prefab to "Elf" and then double-click it in the hierarchy to zoom to it in the Scene view.
Notice the Elf is positioned at 0, 0, 0 within the ThirdPersonController coordinate space, which puts him right on top of Ethan.
Disable EthanBody, EthanGlasses, and EthanSkeleton. Then disable the Elf's Animator component. You will be using the Animator that is attached to the ThirdPersonController instead.
Then click on the target in the Avatar field and select the Elf_MeshAvatar.
Leave the Controller set to the ThirdPersonAnimatorController. This controller has properties that align with the Third Person scripts.
This solution works fine if you want to use Ethan's animations on your Elf instead of the animations you downloaded from Mixamo. Take a look at the ThirdPersonAnimatorController. Double click on the Airborne state to see its blend tree.
Blend trees are beyond the scope of this course, but it is noteworthy that you have just scratched the surface of animating in Unity. Click on the Base Layer tab to return to the base motion states.
You may have noticed that the Skeleton no longer attacks you when you approach it. To fix this, remember that you just need to specify on the SkeletonBehavior script who the Player is. Change the current value of FPSController by dragging in the ThirdPersonController. Then test to make sure the Skeleton will attack.
So what if you want your own animations? You could update the Elf Animation Controller to have the same structure and parameters of the ThirdPersonAnimatorController, but that sounds tedious. You could modify the script that comes with the 3rd Person Character, but that script is pretty complex. You could also not use Unity's ThirdPersonController at all, and instead write your own controller script.