Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement