Search Unity

How can I connect animations together in unity

Discussion in 'Animation' started by HP_tesnn, Sep 24, 2021.

  1. HP_tesnn

    HP_tesnn

    Joined:
    Oct 17, 2020
    Posts:
    11
    In my case, I have 1 animator with many layers. Each layer has an animation. How can I connect these animations together? I want to play these animation by a custom order which is made by user. For example, I have 3 animations 'wave', 'clap' and 'walk'. I want my user can make 3 animations into 1 united animation. These new animation will start from 'walk' then 'wave' then 'clap'. How can I do this?
    Thanks.
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    244
  3. HP_tesnn

    HP_tesnn

    Joined:
    Oct 17, 2020
    Posts:
    11
    Hi. Sorry for the unclear question. What I actually want to know is how to create an animation queue. So users can put any animation in that queue and play it later. I dont want animations to override each other. I want an animation queue in that animations can be played one by one in order. Do I make the question more clearly? Thank you for your response.
     
  4. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    244
    Hey,

    There's no built in functionality to do something like that; you'd have to code it yourself.

    C# has a generic class called a Queue. You could use that to "queue" the various animations and play them back in order.

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.queue-1?view=net-5.0

    I made an example script to give you an idea of where to start. This is untested, and it's NOT meant to be final working code. You will have to do a lot of work on your own to get everything working.

    Here is the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AnimationQueue : MonoBehaviour
    6. {
    7.     Queue<string> animations = new Queue<string>();
    8.  
    9.     Animator anim;
    10.  
    11.     void Start()
    12.     {
    13.         anim = GetComponent<Animator>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (Input.GetKeyDown(KeyCode.Alpha1)) // Press this key to queue a wave animation.
    19.         {
    20.             animations.Enqueue("Wave"); // This is the name of the animator state.
    21.         }
    22.         else if (Input.GetKeyDown(KeyCode.Alpha2))
    23.         {
    24.             animations.Enqueue("Clap");
    25.         }
    26.         else if (Input.GetKeyDown(KeyCode.Alpha3))
    27.         {
    28.             animations.Enqueue("Walk");
    29.         }
    30.  
    31.         if (Input.GetKeyDown(KeyCode.Space)) // Done queuing, play the animations.
    32.         {
    33.             StartCoroutine(PlayAnimations());
    34.         }
    35.     }
    36.  
    37.     IEnumerator PlayAnimations()
    38.     {
    39.         while (animations.Count < 0) // While there are still animations in the queue...
    40.         {
    41.             anim.Play(animations.Peek()); // Plays the first animation in the queue.
    42.  
    43.             animations.Dequeue(); // Removes the first animation in the queue.
    44.  
    45.             yield return new WaitForSeconds(3f); // Waits for 3 seconds.
    46.         }
    47.     }
    48. }
    I've added comments to briefly explain what each line is doing, but the gist of it is that you press the 1-3 keys on the keyboard to queue a different animation, and then play them back with the space key.

    You will need to understand coroutines and learn about queues. There could be mistakes in the above code, it's just meant as an example to give you an idea of where to start.

    If you have any questions, please feel free to ask.
     
unityunity