Skip to content

Instantly share code, notes, and snippets.

@winkels
Last active March 25, 2018 10:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winkels/9770374 to your computer and use it in GitHub Desktop.
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.
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