• Aishwarya Pothula

Unity Tutorial on how to control an Animator

This is a very basic Unity Tutorial on how to control an animator object's behaviour based on user input. For example, the animator object will perform a certain defined action when the user inputs '1'. In the same fashion, the animator object will perform a different action when the user inputs '2' on the keyboard.


Let's get started and create this simple game. You can also watch this video tutorial.






Download and install Unity3D. Open unity and create a new Unity 3D project titled 'Animation Game'.


Configure your unity so that the animator window is visible. To enable, go to 'Window' select 'Animation' and select 'Animator'. You will now be able to see the 'Animator' window available.

Let's talk about the input file that we have on hand. I already have with me an fbx containing a video of a few action sequences. If you don't already have one, you will be able to download fbx files online for free. This Quora page mentions a few resources.


Now, right-click on 'Assets' to 'Import New Asset'. Import the fbx file that you have. Place the asset onto 'Hierarchy' to open it in 'Inspector' view. Click on the video in the assets to see tabs such as 'Model', 'Rig', 'Animation', 'Materials'. When you select the 'Animation' tab and click the play button at the bottom, you will be able to play the video.


We now need to divide the fbx file so that we can define action clips. These clips will the actions that will be performed by the animator object upon user input. We create the first clip by clicking on the video name in the 'Animation' tab and renaming it to "stance" or any other name that you want to give the action. Now, click on 'Clamp Range' to specify the frame ranges in the 'Start' and 'End' input boxes. To create the next clip, click on the "+" symbol below the clips, give the action clip a new name and repeat the procedure.


Once all the required action clips are created, click on 'Create' button in the 'Project' view to create 'Animation Controller'. Give the 'Animation Controller' a name. I've named it Humanoid. Select the asset/fbx file you put in 'Hierarchy' and you will be able to see a 'Controller' box under 'Animator' in the view. Drag and drop the newly created controller-Humanoid onto the 'Controller' box. Once you create the controller you'll be able to see some blocks created in the 'Animator' window. Now, click on the right arrow on the fbx file in the assets under 'Project' view to expand it. You will now be able to see all the action clips that you have created. Drag and drop these clips onto the 'Animator' window. Right click on the 'Animator' screen and select 'Create State' and then select 'Empty' to create an empty state. Right click on the 'entry' box in the 'animator' window and click on ' Make Transition'. As soon as we click on 'Make Transition' an extendable arrow appears. Link the arrow from the 'Entry' state to the 'New State'. Repeat this process until transitions from each of the the clips in the 'Animator' window to the 'New State' are created.


Creating transitions from entry and each of the states/clips to the 'new state' ensures that the animator object returns to the initial state/starting state after performing each action. Without these transitions, the animator object will not be able to repeat an action immediately.

Now, click on the fbx file in the 'Hierarchy' view and click on 'Add Component' in the 'Inspector' view and select 'New Script' to create a new c# script. Now that the script is attached to the game object, we will be able control the behavior of the animator object using the script. Let's name the script 'action'.


Type in the following code


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class animation : MonoBehaviour
{
    public Animator ar;


    void Start()
    {
        ar = GetComponent<Animator>();


    }


    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp("a1"))
        {
            ar.Play("a1");
        }
        if (Input.GetKeyUp("2"))
        {
            ar.Play("a2");
        }
        if (Input.GetKeyUp("3"))
        {
            ar.Play("a3");
        }
        if (Input.GetKeyUp("4"))
        {
            ar.Play("a4");
        }
    }
}


In the code, we create a public animator variable called 'ar' which can be used later to hold the animator object. In 'start' we create an object for the class 'animator'. In the 'update' section we are linking the user input to the actions to be performed by the animator object. Any instructions written in the update section are executed for every frame. We have now successfully created the game. Go to the 'Game' tab and click on play. Now, the game objects perform actions according to the keyboard inputs that we give. Inputting '1' on the keyboard would render action clip 1 being performed by the game object.


You can download the build from the following link.


Build Link

3 views0 comments

Recent Posts

See All

Exporting Individual Fbx Files from Motive Recordings The goal of this blog is to understand how to export individual mocap files from multi-skeleton mocap Motive videos and apply them to a model. You