Hi guys,
I am a newbe to the Unity platform. I have been trying to make an application where an Avatar plays the animation from the comming input string. I managed that by using UnityEditor. However, then I realized that I cannot build my project when UnityEditor is used. Then, I started to scripting again. I found some discussion similar to my case but none of them solves my problem. I have tried to use Animation class but I can not reach the states in order to play them. Then, I tried to use Animator class where now I can reach each state. However, when I try to play the animations in order just the last animation is played. For example;
anim = Character.GetComponent<Animator>();
AnimationClip[] clips = anim.runtimeAnimatorController.animationClips;
anim.Play(clips[0].name);
anim.Play(clips[1].name);
with this script I am only manage to play the second clip but not the first and after second. I also tried to use CrossFade but it give the same result. Is there any way to solve this problem or anyone has any suggestions.
2 Answers
2
Another way to do it is using Animation Events. You can add an animation event to the end of your animation which plays the next animation for you.
Animation Event Guide
You need to play the animation states in serial. Now what you are doing is trying to play them in parallel.
I don’t know the optimal way to do this but this works for me. So try to make this component and attach it to the Game Object where the Animator component is.
NOTICE! This works only if you set the Animation state names same as the corresponding clip names
NOTICE! This very simple code and does not take in to an account the state playback speeds, transition time etc. It uses only the clip length as basis for the transition time to the next state.
using System.Collections;
using UnityEngine;
public class AnimationFlow : MonoBehaviour
{
public Animator animator;
private AnimationClip[] clips;
private void Start()
{
if (!this.GetAnimator())
{
Debug.LogWarning("Cannot find Animator compoment in " + this.gameObject.name + " !! Be sure to attach this script to a GO with Animator and Animator Controller or set it manually");
return;
}
if (!this.GetClips())
{
Debug.LogWarning("No Animator Controller or clips found in " + this.gameObject.name);
return;
}
//you can call this also extrenally if you dont want to play the clips at start.
this.PlayClips();
}
private bool GetAnimator() => this.animator != null || (this.animator = base.gameObject.GetComponentInChildren<Animator>()) != null;
private bool GetClips() => !(this.animator.runtimeAnimatorController == null || (this.clips = this.animator.runtimeAnimatorController.animationClips) == null || this.clips.Length == 0);
public void PlayClips()
{
float cumulativeLength=0;
foreach(AnimationClip clip in this.clips)
{
StartCoroutine(this.PlayClip(Animator.StringToHash(clip.name), cumulativeLength));
cumulativeLength += clip.length;
}
}
IEnumerator PlayClip(int clipHash, float startTime)
{
yield return new WaitForSeconds(startTime);
// Remeber to set you Animator Animation state name same as the Clip name!
this.animator.Play(clipHash);
}
}
Hope you get it working!