Skip to content

Instantly share code, notes, and snippets.

@birdinforest
Created September 9, 2015 05:02
Show Gist options
  • Select an option

  • Save birdinforest/efb9cdbe8d56ec28303f to your computer and use it in GitHub Desktop.

Select an option

Save birdinforest/efb9cdbe8d56ec28303f to your computer and use it in GitHub Desktop.
Play animation when timescale is 0
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