Skip to content

Instantly share code, notes, and snippets.

@makoto-unity
Created May 26, 2016 08:36
Show Gist options
  • Save makoto-unity/466e7a0bd51666eb5fe084cb97bbb007 to your computer and use it in GitHub Desktop.
Save makoto-unity/466e7a0bd51666eb5fe084cb97bbb007 to your computer and use it in GitHub Desktop.
Unity Animation sequencer
using UnityEngine;
using System.Collections;
using UnityEngine.Assertions;
[RequireComponent(typeof(Animator))]
public class AnimationSequencer : MonoBehaviour {
// Use this for initialization
private Animator myAnim;
float nowLimit;
float maxLength;
float minLength = 0.4f;
void Start () {
myAnim = GetComponent<Animator>();
Assert.IsNotNull(myAnim);
//StartCoroutine(StopAnimation(0.5f));
nowLimit = minLength;
AnimatorStateInfo currentState = myAnim.GetCurrentAnimatorStateInfo(0);
maxLength = currentState.length / 2.0f + minLength;
}
void Update()
{
AnimatorStateInfo currentState = myAnim.GetCurrentAnimatorStateInfo(0);
if (currentState.length < Mathf.Infinity)
{
float absSpeed = Mathf.Abs(myAnim.GetFloat("speed"));
float duration = currentState.normalizedTime * currentState.length * absSpeed / 2.0f;
print(duration);
if (myAnim.GetFloat("speed") > 0.0f)
{
if (duration > nowLimit)
{
if ( isPushing ) {
nowLimit += 1.0f;
if (nowLimit > maxLength)
{
myAnim.SetFloat("speed", 0.0f);
nowLimit = maxLength;
}
} else {
myAnim.SetFloat("speed", 0.0f);
}
}
}
else
{
if (duration < nowLimit)
{
if ( isPushing ) {
nowLimit -= 1.0f;
if (nowLimit < minLength)
{
myAnim.SetFloat("speed", 0.0f);
nowLimit = minLength;
}
} else {
myAnim.SetFloat("speed", 0.0f);
}
}
}
}
}
public void OnNextButton()
{
if (myAnim.GetFloat("speed") > 0.0f) return;
if (nowLimit >= maxLength)
{
nowLimit = maxLength;
}
else
{
myAnim.SetFloat("speed", 8.0f);
nowLimit += 1.0f;
nowLimit = Mathf.Min(nowLimit, maxLength);
}
}
public void OnPrevButton()
{
if (myAnim.GetFloat("speed") < 0.0f) return;
if (nowLimit <= 0.0f)
{
nowLimit = 0.0f;
}
else
{
myAnim.SetFloat("speed", -8.0f);
nowLimit -= 1.0f;
nowLimit = Mathf.Max(nowLimit, minLength);
}
}
private bool isPushing = false;
public void OnNextButtonPushStart()
{
isPushing = true;
OnNextButton();
}
public void OnNextButtonPushEnd()
{
isPushing = false;
}
public void OnPrevButtonPushStart()
{
isPushing = true;
OnPrevButton();
}
public void OnPrevButtonPushEnd()
{
isPushing = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment