/* * returns "true", if the animation was changed * returns "false", if the animation is already playing or not set */ public virtual bool setCurrentAnimation(EMovementState newState, bool interrupt) { if (currentState == newState) return false; else currentState = newState; if (interrupt) { print("anim interrupted"); // clear pending anim, if set before bHasPendingAnimation = false; pendingAnim = null; // switch to new animation directly currentAnim = getSpriteAnimation(newState); currentAnim.startAnimation(); framesPerSecond = currentAnim.framesPerSecond; } else { // check, if we already have a pending animation // in this case, we do not need to call "end animation" again and just override pendingAnim if (!bHasPendingAnimation) { print("made Pending"); bHasPendingAnimation = true; currentAnim.endAnimation(); } // remember the next animation pendingAnim = getSpriteAnimation(newState); } return true; }
void Update() { print(currentState); if ((Time.time - lastFrameUpdateTime) * framesPerSecond >= 1.0f) { if (bHasPendingAnimation && currentAnim.hasFinishedEndAnimation()) { print("switch to pending anim"); Debug.Assert(pendingAnim != null, "Animator is pending, but has not pending animation set!"); bHasPendingAnimation = false; currentAnim = pendingAnim; pendingAnim = null; // clear, to be sure currentAnim.startAnimation(); framesPerSecond = currentAnim.framesPerSecond; } // get the next frame lastFrameUpdateTime = Time.time; spRenderer.flipX = lookLeft; spRenderer.sprite = currentAnim.getNextFrame(); } }
void Start() { currentAnim = IdleAnim; spRenderer.flipX = lookLeft; spRenderer.sprite = currentAnim.startAnimation(); framesPerSecond = currentAnim.framesPerSecond; lastFrameUpdateTime = Time.time; }