Created
September 9, 2015 05:02
-
-
Save birdinforest/efb9cdbe8d56ec28303f to your computer and use it in GitHub Desktop.
Play animation when timescale is 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class PlayAnimationWithoutTimeScale : MonoBehaviour { | |
| public Animation targetAnimation; | |
| public string stateName; | |
| AnimationState targetState; | |
| float lastRealTime = 0.0f; | |
| void Start(){ | |
| if(targetAnimation == null) | |
| targetAnimation = GetComponent<Animation>(); | |
| if(targetAnimation != null) | |
| { | |
| targetState = targetAnimation[stateName]; | |
| } | |
| } | |
| void OnEnable(){ | |
| lastRealTime = Time.time; | |
| } | |
| // This is one of the few events called regularly while Time.timeScale is 0. | |
| void Update () { | |
| if(targetState == null || Time.timeScale != 0) | |
| return; | |
| targetState.weight = 1; | |
| targetState.enabled = true; | |
| targetState.time += (Time.realtimeSinceStartup - lastRealTime); | |
| lastRealTime = Time.realtimeSinceStartup; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment