Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Playing animation w/Time.timeScale = 0

Discussion in 'Scripting' started by User340, May 9, 2011.

  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Is it possible to play an animation while Time.timeScale == 0? I'm trying to play a menu animation when the game is paused.
     
  2. Deleted User

    Deleted User

    Guest

  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Also worth mentioning is Time.realtimeSinceStartup. This gives the actual game time whereas Time.time gives the time as modified by the timescale.
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    @technicat: Thanks for the info about iTween.

    @andeeee: Yes, thanks for the link. I was considering modifying AnimationState.time based on the variable you referenced (instead of Animation.Play ()). But would prefer a nicer way to do it.
     
    Last edited: May 10, 2011
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Alright, I got it to work. I'm using Time.realtimeSinceStartup along with function Update.


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class AnimationExtras : MonoBehaviour {
    6.    
    7.     ArrayList states = new ArrayList ();
    8.     float lastRealTime = 0.0f;
    9.    
    10.     public void PlayOnce (AnimationState state) {
    11.         states.Add (state);
    12.     }
    13.    
    14.     // This is one of the few events called regularly while Time.timeScale is 0.
    15.     void Update () {
    16.        
    17.         for (int i = 0; i < states.Count; ++i) {
    18.            
    19.             AnimationState state = (AnimationState)states [i];
    20.             state.weight = 1;
    21.             state.enabled = true;
    22.             state.time += (Time.realtimeSinceStartup - lastRealTime);
    23.            
    24.             if (state.time >= state.length)
    25.                 states.Remove (state);
    26.         }
    27.         lastRealTime = Time.realtimeSinceStartup;
    28.     }
    29.    
    30. }
     
    Katunator likes this.
  6. deepak

    deepak

    Joined:
    Aug 10, 2010
    Posts:
    44
    Hi

    When we pause the game using "Time.timescale = 0" and then resume after some minutes, the collider were missing from the character animation. We are using five mesh collider for the character. Any solutions?

    Thanks in advance