Animator C# or Animation

I am trying to write a script (C#) for a dynamic playlist of moves (think make your own dance). After reading a lot of the forums, I have come to the conclusion, I have no idea if I should use Animator or Animation. The overall play would be:

  1. User clicks buttons to ‘create a playlist’ of motions on a tablet. (moves[step_L, step_R, jump, step_L, step_right, twirl] )
  2. User hits play and the clips play on the Oculus.

I feel like it should be simple enough. I am currently using the animator, but can’t figure out how to play specific clips from the animator, and in the documentation I can only find out how to play from the Animator in C#.

I also wouldn’t be opposed to restructuring my approach if someone has a more efficient method.

using UnityEngine;
using System.Collections;

public class anim_Playlist : MonoBehaviour {

    public Animator animator;
    public AnimationClip[] animPlayList;
    int currClip = 0;


    //----------------------
    //----Start-------------
    //----------------------
    void Start()
    {
        // hardcode number of clips in playlist
        int numberOfClips = 4;

        // create an array the size of the playlist that will later pull from database
        string[] dataBase_string;
        dataBase_string = new string[numberOfClips];

        // hardcode some strings that will eventually be read in from the database
        dataBase_string[0] = "crouch-ready"; // should be the hash of this 'state'
        dataBase_string[1] = "crouch-set";
        dataBase_string[2] = "crouch-run";
        dataBase_string[3] = "run";

        // get animator
        animator = GetComponent<Animator>();


        // Go through each clip in the animator
        foreach (AnimationClip clip in animator.runtimeAnimatorController.animationClips)
        {
            // Check the string name against play list from database
            foreach (string stringName in dataBase_string) {
                if (clip.name == stringName)
                {
                    // if the names are the same, add the clip to the array of anim clips
                    animPlayList[currClip] = clip;
                    currClip++;
                }
            }
        }
    }

    //----------------------
    //----Update------------
    //----------------------
    void Update()
    {
        // I want to play the playlist when space is pressed
        if (Input.GetKeyDown("space"))
        {
            for (int i = 0; i < 4; i++)
            {
                //something like this
                animPlayList[i].play();
                //yield return new WaitForSeconds(anim.clip.length);
            }
        }
    }
}

I’m no pro at animators but this is my 2 cents.

The way I would tackle this is the following.

1.) Create a DanceStateMachine

  • This script should handle switching of animations and give you the current clip being played if needed for a time delay from the clip.length property.
    2.) Create an Animator and create an Empty State.
    X.) [Optional] If your moves have ‘categories’ break them up into their respective Substates and transition into those.
    3.) Transition from the Empty State to the Animation that you desire, personally for me I like to use Enums and set an int value to do the transitions.
    Example: animator.SetInteger(this.m_DanceMove.ToString(), (int)this.m_DanceMove);
    where m_DanceMove is an enum DanceMove { NONE = 0, WIGGLE, SHAKE_BOOTY… }

This would be my approach I have done this before and its easy but it does take some setup and you HAVE to have a set of rules for your animation setup. i.e. WIGGLE = 1 so when you transition with the Integer make it DanceMove EQUALS 1. This is hardcoded but its the best solution as it is much faster then comparing strings.