JustToThePoint English Website Version
JustToThePoint en español
JustToThePoint in Thai

How to animate with Mixamo and Unity

You must not let anyone define your limits because of where you come from. Your only limit is your soul, Gusteau, Ratatouille.

We live in a visual world where people are less interested in reading and more likely to view something that feeds the eyes and excites the imagination. Videos and animations are the most efficient and attractive ways for any learning process, marketing strategy or any other kind of communication because they are highly engaging and entertaining. They both easily grab the attention of the audience. Animation is the visual art of bringing life to inanimate objects, illustrated or 3D generated characters. How to animate with Mixamo and Unity

How to animate with Mixamo and Unity

 

Mixamo & Unity: animations for our games

Mixamo is a free online service that has a vast collection of characters, animations, and tools for easily rigging and animating 3D characters. Unity is one of the best platform for creating videogames.

Rigging is the process of creating a virtual skeleton that defines how different parts of the model’s body are connected and how they move related to each other.
Mixamo

Mixamo

 

  1. Download and install Unity Hub, launch it, and create a new project by clicking the Create project button, then 3D (Core), give it a name (e.g., MyFirstProject), and press the Create project button again. Unity & Mixamo

    Unity & Mixamo

     

  2. Once you are logged in to the Mixamo webpage, pick a character that you like, e.g., Sophie, and click the Download button. Select in the Format dropdown menu, the FBX for Unity (fbx) option, and in Pose, T-pose. I am going to rename the downloaded file as Sophie.fbx.

  3. Switch to the Animation section and select some animations, e.g, Idle, Walking, Running, or Hip Hop Dancing, and for each of these animations click the Download button and select the following export options: Format (FBX for Unity), Skin (Without Skin -it means that we don’t want to download the model again, just the animation), Frames per Second (30 is OK).

  4. Importing some assets. Log in to the Unity Asset Store website. Search for Modular Lowpoly Streets (Free), click on the Add to My Assets button, then Open in Unity, finally a pop up window will ask if you want to Open Unity Editor, tick on the Always allow assetstore.unity.com to open links of this type in the associated app checkbox, and smash the Open Unity Editor button. It will open the asset inside the Unity editor. Unity & Mixamo

    Unity & Mixamo

     

  5. The package manager (Windows, Package Manager) displays which packages are available for installation or are already installed in your project, so just click Download to download this asset, then Import. Next, a dialog box would appear (Import Unity Package), click All, then Import, and the Import Unity Package window will automatically close when the process is finished.

    You should be able to see the asset package listed in its own ModularLowpolyStreetsFree folder in the Assets Folder, in the Project window. Drag and drop the demo scene located in the Scenes Folder of this asset (Project, Assets, ModularLowpolyStreetsFree, Scenes, Demo) to our Hierarchy window.

    The Hierarchy is where you need to place and organize all your GameObjects such as cameras, lights, and characters in the game’s active scene. The Project windows is where you will find all the assets and files available for the project.

    Unity & Mixamo

    Unity & Mixamo

     

  6. Let’s import Mixamo’s model and animations in our Unity project by dragging them in the “Assets” folder.

  7. Let’s drag our Sophie model into the Scene, double click on it to zoom in. Next, we need to configure some settings. Go to the Inspector, switch to the Rig tab, and select in the Animation Type dropdown, the Humanoid option, and in the Avatar Definition dropdown, the Create From This Model option. Finally, press the Apply button.

    When a game object is selected, the Inspector displays all of the publicly accessible properties and components specific to this object.
    Unity & Mixamo

    Unity & Mixamo

     

  8. Click on the Hip Hop Dancing animation, go to the Inspector, switch to the Rig tab, and select in the Animation Type dropdown, the Humanoid option; in the Avatar Definition dropdown, the Copy From Other Avatar option, and in the Source dropdown, the SophieAvatar (we are going to reuse Sophie’s model avatar), then click Apply. After that, switch to the Animation tab, and check Loop Time (if Loop Time is enabled, then the animation loops forever). Finally, smash the Apply button. We will repeat the process with our walking animation.

    SophieAvatar is an asset used to map and configure the bones of our model, i.e., it contains the rig definition of our model. Unity will automatically create an avatar from our downloaded Mixamo’s model file (fbx).
    Unity & Mixamo

    Unity & Mixamo

     

  9. Let’s create an Animator Controller, that is, a Unity asset that controls the logic of an animated GameObject. It lets you arrange and link various animations together using a state machine.Right click on the assets section, Create, Animator Controller and let’s rename it as SophieController.

  10. Double click on SophieController. Drag and drop first the Walking animation and then the Hip Hop Dance animation to the Animator Controller, because we want Sophie to walk, then dance immediately afterwards.

    When you drag and drop the Walking animation to the Animator Controller, it is automatically connected to the Entry animation state. It means that it is automatically set as the default animation state.
    Unity & Mixamo

    Unity & Mixamo

     

  11. Let’s go back to our 3D model, select Sophie on the top left Hierarchy, and then drag and drop our recently created Animator Controller, SophieController, in the Inspector, more specifically in the Animator section, in the Controller box. Unity & Mixamo

    Unity & Mixamo

     

  12. Finally, go to the Scene view, select the Main Camera (select in the Game Object, the option Align with View to adjust the main camera’s position); then, go to Game, and click on play to see the animation.

    Play mode is one of Unity’s core features. It allows you to run and test the progress of your current game directly inside the Editor, via the Play button. Pressing the play button for a second time will exit Play mode and return to the scene view.
    Unity & Mixamo

    Unity & Mixamo

     

How to animate and move a simple player with Unity

  1. Go to the Hierarchy and select our model Sophie. Go to the inspector, in the Animator section, select in the Update Mode dropdown, the Animate Physics option. The animator is updated in sync with Unity’s physics system. How to animate and move a simple player with Unity

    How to animate and move a simple player with Unity

     
  2. Double click our Animator Controller SophieController. The Parameter tab in the left pane of the animator controller will allow you to control transitions between different states. Let’s add a parameter by pressing the plus + sign in the top right, then select Bool, and then rename it as IsWalking. We will also create a Jump trigger.
  3. Drag and drop the Idle, walking, and Jumping animations (Go to Mixamo and download these animations with the In Place box checked when viewing them) to the Animator Controller. We are going to create a conditional transition between two states: Idle and Walking. Right click on the Idle animation/state and select Make Transition, then click on the Walking animation/state.
  4. Click on the recently created transition, in the Conditions section, click on the + icon to automatically create a new condition for this transition between Idle and Walking: =isWalking, true. Besides, create a transition between Walking and Idle, and the condition for this transition should be =isWalking, false. How to animate and move a simple player with Unity

    How to animate and move a simple player with Unity

     
  5. Create a transition between Walking and Jumping (the condition is the Jump trigger), and between Jumping and Walking.
  6. In the hierarchy, select our model/character Sophie. In the inspector, select Add Component, type in script, and select the New script option, and name it PlayerController. Double click the script to open it. We need to create a reference to both our Animator and the Rigidbody. Rigidbodies are components that allow a GameObject such as our model Sophie, to react to real-time physics, i.e., forces and gravity, mass, drag and momentum.
    using UnityEngine;
    
    [RequireComponent(typeof(Animator))]
    [RequireComponent(typeof(Rigidbody))]
    public class PlayerController : MonoBehaviour
    {
        private Animator _animator;
        private Rigidbody _rigidbody;
    }
    
  7. Go back to the Unity editor, you will see that the Rigidbody has been automatically added to our model/game object Sophie. Click on Add Component to add a Box Collider so the model/game object collides with the ground, stays there, and does not just keep falling to the deep of the abyss. Make sure that the Box Collider is properly sized to match the game object dimensions.
  8. Let’s go back to the code. The start function will be called just one time when the game object is instantiated, that is, immediately when pressing the play button. The Update method is called every single frame that the game is running and the game object that the script is attached to is active.
using UnityEngine;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(Rigidbody))]
public class PlayerScript : MonoBehaviour
{
    private Animator _animator;
    private Rigidbody _rigidbody;
    private float _horizontalInput;
    private float _verticalInput;
    private static float _SPEED = 0.3f;
    private bool _IsWalking;
    public float thrust = 1.0f;

    // Start is called before the first frame update
    void Start()
    {
        _animator = GetComponent<Animator>();
        _rigidbody = GetComponent<Rigidbody>();
	_IsWalking = false;
    }

    // Update is called once per frame
    void Update()
    {
	_horizontalInput = Input.GetAxis("Horizontal");
        _verticalInput = Input.GetAxis("Vertical");
	if ( Input.GetButtonDown("Jump")  )

Input.GetAxis(“Horizontal”) return the value of the virtual axis identified as Horizontal. It will be in the range -1 (the left arrow key or “a” is pressed).. 0 (no key is pressed).. 1 (the right arrow key or “d” is pressed). How to animate and move a simple player with Unity

How to animate and move a simple player with Unity

 

Input.GetButtonDown(“Jump”) returns True when the button Jump is held down, by default the button Jump is the space bar.

The Input manager windows (Edit, Project Settings, Input Manager) is where you can configure the controls to use with the UnityEngine.Input.

	{
            	_animator.SetTrigger("Jump"); 
                // It sets an animation trigger and cause a change in flow in the state machine of our Animator Controller, from Walking to Jumping. A trigger gets consumed (turned to false) immediately after it is used.
	        _rigidbody.AddForce(0, 0, thrust, ForceMode.Impulse); // It applies an impulse force along the Z axis to the GameObject's Rigidbody.
        }
        else if (Mathf.Abs(_horizontalInput) > 0.001f)
        // If horizontalInput is not null (with some small threshold for some approximation errors with floats)...
        {
            //... we need to move Sophie. First, we create a rotation to look either left or right depending on the User's input (_horizontalInput); 
	    transform.rotation = Quaternion.LookRotation(new Vector3(-_horizontalInput, 0f, 0f));
	    // Then, we move forward the GameObject while also considering its rotation using transform.forward (d=d0+v*t+1/2*a*t^2 and a=0)  
            _rigidbody.MovePosition(_rigidbody.position - transform.forward * _SPEED * Time.fixedDeltaTime);

            if (!_IsWalking )
            {
                _IsWalking = true;
                _animator.SetBool("IsWalking", true); 
                // It sets the value of the Animation Controller's "IsWalking" parameter as true, switches from the Idle to the Walking state, and triggers the Walking animation.
                // Please notice that the Walking Animation will always be active in loop from now on.
            }   
        }
	else if (Mathf.Abs(_verticalInput) > 0.001f) {
        // If the user presses the up or down arrows or "w" or "s", it sets the value of the Animation Controller's "IsWalking" parameter as false, switches from the Walking to the Idle state, and triggers the Idle animation.
        // We could have checked if the attribute _IsWalking is True, but it is not necessary.
	    _IsWalking = false;
            _animator.SetBool("IsWalking", false);	
	}
}

Credits: This article is inspired by How to animate and move a simple player with Mixamo and Unity/C#

Bitcoin donation

JustToThePoint Copyright © 2011 - 2022 Anawim. ALL RIGHTS RESERVED. Bilingual e-books, articles, and videos to help your child and your entire family succeed, develop a healthy lifestyle, and have a lot of fun.

This website uses cookies to improve your navigation experience.
By continuing, you are consenting to our use of cookies, in accordance with our Cookies Policy and Website Terms and Conditions of use.