Development (Advanced)


Animation system overview

Unity has a rich and sophisticated animation system (sometimes referred to as Mecanim). While Mecanim is recommended for use in most situations, Unity has retained its legacy animation system which existed before Unity 4. Because you may need to use the legacy when working with older content created before Unity 4.

Here, the Mecanim, shown in the figure above, is explained mainly and legacy is also guaranteed:

  1. Animation Clips are one of the core elements to Unity’s animation system. Unity supports importing animation from external sources. The Animation Clips and the Animator Controller are brought together on a GameObject via the Animator Component.

  2. This manages the various animation states and the transitions between them using a so-called State Machine, which could be thought of as a kind of flow-chart, or a simple program written in a visual programming language within Unity.

  3. The animation controller has references to the animation clips used within it.

  4. The model is imported at run time. When a GLTFComponent is created, the animation controller is connected to the specific game object (rig_GRP).

Step 1: Animator controller

To apply the animation to the model, you must have a basic knowledge of the animation controller. Developers can easily control animation with it.

An Animator Controller allows you to arrange and maintain a set of Animation Clips and associated Animation Transitions for a character or object. In most cases it is normal to have multiple animations and switch between them when certain game conditions occur. For example, you could switch from a walk Animation Clip to a jump Animation Clip whenever the spacebar is pressed. However even if you only have a single Animation Clip you still need to place it into an Animator Controller to use it on a GameObject.

The Animator Controller has references to the Animation clips used within it, and manages the various Animation Clips and the Transitions between them using a State Machine, which could be thought of as a flow-chart of Animation Clips and Transitions, or a simple program written in a visual programming language within Unity.

Here, the model is loaded runtime, and then an animation controller component is added to the model. So, we will learn how to make an animation clip and apply a simple animation through a State Machine.

Runtime Animation Controller of GLTFComponent.cs

GameObject o = GameObject.Find("rig_GRP");
Animator animator = o.AddComponent<Animator>();
RuntimeAnimatorController controller = Resources.Load<RuntimeAnimatorController>("AnimController");
animator.runtimeAnimatorController = controller

Step 2: Make the animation clip

Animation Clips are one of the core elements to Unity’s animation system. Unity supports importing animation from external sources, and offers the ability to create animation clips from scratch within the editor using the Animation window.

Animation data collection

We provided a tool to make what works in our model. Before then, we need animation data. It can be made directly with a professional tool like Maya and 3Ds Max or it can be obtained for free like Mixamo (https://www.mixamo.com/).

An animation designed file (*.fbx) should be created as an animation clip (*.anim) for unity.

The animation clip can be generated using Unity Studio. It will be supported later by Windows and Mac operating systems.

  1. Import package

  2. Check imported AnimationGenToolScene

    Assets -> Scenes

  3. Game Window

    If you adjust the scale (1x), you can see the screen below.

  4. Click the Create .Anim button.

    Windows Explorer will show up.

    a. Select the *.fbx file.

    b. When completed, the following information is displayed.

    c. Verify the generated *.anim file.

  5. If you want to check the animation, click the Load Model button.

Step 3: Animation state machine

Unity’s State Machine provides a way to overview all of the animation clips related to a particular character and allow various events in the game (for example, user input) to trigger different animations.

State Machine Transitions exist to help you simplify complex State Machines. This allows you to have a higher level of abstraction over the state machine logic. Each view in the animator window has an Entry and Exit node. The animator window specifies the State Machine Transitions. The Entry node is used when transitioning into a state machine. The entry node will be evaluated and will branch to the destination state according to the conditions set. In this way, the entry node can control which state the state machine begins in, by evaluating the state of your parameters when the state machine begins. Because the state machine always has a default state, there will always be a default transition branching from the entry node to the default state.

Step 4: Walking animation system

Here, we use a walking animation clip. You can find it here:

Project -> Assets -> Resources -> Mecanim -> Walk.anim

  1. The Walk animation clips are placed and arranged in an Animator Controller.

    a. Double click the AnimController, then you can see the Animator window

    b. Next, apply the animation by dragging the Walk animation to the button.

  2. Make the transition from Idle to Walk.

    • Right mouse button to click the “Idle” node.

    • Connect lines from “Idle” node toward “Walk” node.

  3. Select the connect line for transition (Idle → Walk)

    a. Unchecking

    b. Set the parameter to Bool

    Animation Parameters are variables that are defined within an Animator Controller that can be accessed and assigned values from scripts. This is how a script can control or affect the flow of the state machine.

    c. Add the condition of transition (Idle → Walk)

  4. Select the connect line for transition (Idle ← Walk)

    To be repeated in the same way as Step 3. However the condition of walk is set to False.

  5. Writing Script

  6. Script that modifies parameters based on user input ButtonBehaviour.cs

    public class ButtonBehaviour : MonoBehaviour
    {
        public Animator anim;
     
       // Update is called once per frame
        void Update()
        {
            if (anim == null)
            {
                GameObject o = GameObject.Find("rig_GRP");
                if (o != null)
                {
                    anim = o.GetComponent<Animator>();
                }
    
            }
            else
            {
                if (Input.GetKey(KeyCode.RightArrow))
                {
                    transform.rotation = Quaternion.Euler(0, -90, 0);
                    anim.SetBool("walk", true);
                }
                else if (Input.GetKey(KeyCode.LeftArrow))
                {
                    transform.rotation = Quaternion.Euler(0, 90, 0);
                    anim.SetBool("walk", true);
                }
                else if (Input.GetKey(KeyCode.DownArrow))
                {
                    transform.rotation = Quaternion.Euler(0, 0, 0);
                    anim.SetBool("walk", false);
                }
                else
                {
                    anim.SetBool("walk", false);
                }
            }
        }
    }
    
  7. Play and Review the Walk Animation.

Then push the right-button of the keyboard, to make your AR Emoji character move to the right using the Walk animation

After Walk Animation, the state of the AR Emoji will be back to Idle animation.

You're done!

Congratulations! You have successfully achieved the goal of this Code Lab activity. Now, you can create AR Emoji and use it as a game character all by yourself! But, if you're having trouble, you may check out the link below.

AR Emoji Complete Code183.37 MB