private void PlayToState(State state) { // set the animation time that we want to play to targetAnimationEndTime = GetNormalizedTimeForState(state); // note that the normalizedTime always resets to zero after finishing the clip. // so if button was at Pressed and it was already done playing, its normalizedTime is // 0f, even though the Pressed state corresponds to a time of 1f. so, for this special // case, force it to 1f. if (CurrentState == State.Pressed && animationState.normalizedTime == 0f && !ButtonAnimation.isPlaying) { animationState.normalizedTime = 1f; } // move either up or down depending on where the switch is right now animationState.speed = Mathf.Sign(targetAnimationEndTime - animationState.normalizedTime) * 1f; // play animation and actuate switch ButtonAnimation.Play(animationName); SetState(state); // play sound effect if available if (SoundEffect != null) { SoundEffect.Play(); } }
private void GoToState(State state) { SetState(state); // switch to animation state instantly animationState.normalizedTime = GetNormalizedTimeForState(state); animationState.speed = 0f; ButtonAnimation.Play(animationName); }