Advertisement
Guest User

Untitled

a guest
May 26th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Assertions;
  4.  
  5. [RequireComponent(typeof(Animator))]
  6. public class AnimationSequencer : MonoBehaviour {
  7.  
  8. // Use this for initialization
  9. private Animator myAnim;
  10. float nowLimit;
  11. float maxLength;
  12. float minLength = 0.4f;
  13.  
  14. void Start () {
  15. myAnim = GetComponent<Animator>();
  16. Assert.IsNotNull(myAnim);
  17. //StartCoroutine(StopAnimation(0.5f));
  18. nowLimit = minLength;
  19. AnimatorStateInfo currentState = myAnim.GetCurrentAnimatorStateInfo(0);
  20. maxLength = currentState.length / 2.0f + minLength;
  21. }
  22.  
  23. void Update()
  24. {
  25. AnimatorStateInfo currentState = myAnim.GetCurrentAnimatorStateInfo(0);
  26. if (currentState.length < Mathf.Infinity)
  27. {
  28. float absSpeed = Mathf.Abs(myAnim.GetFloat("speed"));
  29. float duration = currentState.normalizedTime * currentState.length * absSpeed / 2.0f;
  30. print(duration);
  31. if (myAnim.GetFloat("speed") > 0.0f)
  32. {
  33. if (duration > nowLimit)
  34. {
  35. if ( isPushing ) {
  36. nowLimit += 1.0f;
  37. if (nowLimit > maxLength)
  38. {
  39. myAnim.SetFloat("speed", 0.0f);
  40. nowLimit = maxLength;
  41. }
  42. } else {
  43. myAnim.SetFloat("speed", 0.0f);
  44. }
  45. }
  46. }
  47. else
  48. {
  49. if (duration < nowLimit)
  50. {
  51. if ( isPushing ) {
  52. nowLimit -= 1.0f;
  53. if (nowLimit < minLength)
  54. {
  55. myAnim.SetFloat("speed", 0.0f);
  56. nowLimit = minLength;
  57. }
  58.  
  59. } else {
  60. myAnim.SetFloat("speed", 0.0f);
  61. }
  62. }
  63. }
  64. }
  65. }
  66.  
  67. public void OnNextButton()
  68. {
  69. if (myAnim.GetFloat("speed") > 0.0f) return;
  70. if (nowLimit >= maxLength)
  71. {
  72. nowLimit = maxLength;
  73. }
  74. else
  75. {
  76. myAnim.SetFloat("speed", 8.0f);
  77. nowLimit += 1.0f;
  78. nowLimit = Mathf.Min(nowLimit, maxLength);
  79. }
  80. }
  81.  
  82. public void OnPrevButton()
  83. {
  84. if (myAnim.GetFloat("speed") < 0.0f) return;
  85. if (nowLimit <= 0.0f)
  86. {
  87. nowLimit = 0.0f;
  88. }
  89. else
  90. {
  91. myAnim.SetFloat("speed", -8.0f);
  92. nowLimit -= 1.0f;
  93. nowLimit = Mathf.Max(nowLimit, minLength);
  94. }
  95. }
  96.  
  97. private bool isPushing = false;
  98.  
  99. public void OnNextButtonPushStart()
  100. {
  101. isPushing = true;
  102. OnNextButton();
  103. }
  104.  
  105. public void OnNextButtonPushEnd()
  106. {
  107. isPushing = false;
  108. }
  109.  
  110. public void OnPrevButtonPushStart()
  111. {
  112. isPushing = true;
  113. OnPrevButton();
  114. }
  115.  
  116. public void OnPrevButtonPushEnd()
  117. {
  118. isPushing = false;
  119. }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement