Last active
March 25, 2018 10:45
-
-
Save winkels/9770374 to your computer and use it in GitHub Desktop.
Unity script that allows for animation while Time.timeScale = 0. See http://www.asteroidbase.com/?p=569 for context.
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 TimeScaleIndependentAnimation : TimeScaleIndependentUpdate | |
{ | |
//inspector fields | |
public bool playOnStart; | |
public string playOnStartStateName; | |
//private fields | |
AnimationState currentState; | |
System.Action currentCompletionHandler; | |
float elapsedTime; | |
bool playing; | |
void Start() | |
{ | |
if(playOnStart) | |
{ | |
Play(playOnStartStateName); | |
} | |
} | |
protected override void Update() | |
{ | |
base.Update(); | |
if(playing) | |
{ | |
elapsedTime += deltaTime; | |
currentState.normalizedTime = elapsedTime / currentState.length; | |
if(elapsedTime >= currentState.length) | |
{ | |
playing = false; | |
if(currentState.wrapMode == WrapMode.Loop) | |
{ | |
Play(currentState.name); | |
} | |
else | |
{ | |
if(currentCompletionHandler != null) | |
{ | |
currentCompletionHandler(); | |
} | |
} | |
} | |
} | |
} | |
public void Play(string stateName, System.Action completionHandler = null) | |
{ | |
elapsedTime = 0f; | |
currentState = animation[stateName]; | |
currentState.normalizedTime = 0; | |
currentState.enabled = true; | |
currentState.weight = 1; | |
currentCompletionHandler = completionHandler; | |
playing = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment