Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by equalsequals · Nov 03, 2010 at 10:00 PM · timeanimationspause

Ideas for Animating while Paused?

Hey all,

The current project I am working on has animated menus, which are not programmatic but instead use Unity's Animation tool. There are also nicely animated transitions that occur between screens, also utilizing the Animation tool.

We are using Time.timeScale and setting it to 0 as a solution for pausing the game, in which case the Animations stop as well. This doesn't surprise me and it makes sense why this would happen, but it does cause problems when we're trying to play these animations during the pause screen.

Does anyone have a solution for this? I've done some digging for this and it seems like everything I have found would work for programmatic animations, but not Unity's Animation Clips.

Thanks in advance.

==

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lodendsg · May 23, 2015 at 12:16 PM 1
Share

To help others notice this option though its not the accepted answer Animator Update $$anonymous$$ode --> UnscaledTime That will give you the desired result with no coding required as noted by @Salazar below

9 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by equalsequals · Nov 04, 2010 at 06:57 PM

First off I wanted to thank @Eric5h5, @Paulius Liekis and @Herman Tulleken for the answers. I actually used a mixture of solutions.

Basically, I had so much code invested in FixedUpdate, etc. That the pausing solution needed to remain the way it was. Inside my base animation manager class I adjusted the code so that animations run independently of Time.timeScale. I used a spin of Paulius and Herman's methods to achieve this.

This class is a more basic version of what I had to do to get this to work, it involves 2 animations, an in transition and an out transition.

using UnityEngine; using System.Collections;

[RequireComponent(typeof(Animation))] public class EQEQAnimationManager : MonoBehaviour { //for calculating our delta time float _timeAtLastFrame = 0F; float _timeAtCurrentFrame = 0F; float deltaTime = 0F;

 AnimationState _currState;

 bool isPlaying = false;
 float _startTime = 0F;
 float _accumTime = 0F;

 void Update()
 {
     //we create our own deltaTime based on realtimeSinceStartup here
     _timeAtCurrentFrame = Time.realtimeSinceStartup;
     deltaTime = _timeAtCurrentFrame - _timeAtLastFrame;
     _timeAtLastFrame = _timeAtCurrentFrame; 

     if(isPlaying) AnimationUpdate();
  }

  void AnimationUpdate()
  {
     //accumulate time
     _accumTime += deltaTime;
     //normalized time is from 0, the animation's beginning to 1, the end. We'll set it where the accumulated time is in the current animation's duration.
     _currState.normalizedTime = _accumTime/_currState.length; 
     if(_accumTime >= _currState.length) 
     {
         OnAnimationCompleted();
         _currState.enabled = false;
         isPlaying = false;
     }
  }

  public void PlayAnimation(string suffix)
  {
     _accumTime = 0F;
     _currState = animation[gameObject.name + " " + suffix]; //my animations are organized by the name of the game object, and what the animation is (ie "Main Menu Transition In" in which suffix would be "Transition In")
     _currState.weight = 1;
     _currState.blendMode = AnimationBlendMode.Blend;
     _currState.wrapMode = WrapMode.Once;
     _currState.normalizedTime = 0;
     _currState.enabled = true;
     isPlaying = true;
  }

  internal void OnAnimationCompleted()
  {
     //hook for end of animation
  }

}

In this case, I took advantage of Animation States and AnimationState.normalizedTime, using my custom deltaTime (thanks again Herman) to allow the animation to play as expected even if Time.timeScale is set to 0. Since these are straight-forward, linear animations, I was able to get away with setting the AnimationState's values in PlayAnimation the way I did. In other cases I'd probably want to inject more parameters.

What I also noticed was that I lost connections to my AnimationEvents which I inserted using Unity's Animation Editor. In my case it was a single function call which was fired on completion. Again, because of the linear nature of these animations I was able to get away with just calling that function after the accumulated time reached the length of the animation in my custom update loop.

I hope this helps anyone else with a similar complication. I was lucky that I only had to modify a single class and everything worked as expected. I have good class and package structure to thank for that. :)

Chances are you may need to modify the code above to fit your needs but I hope it serves as a stable launch point for you.

Cheers

==

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GigaDeath · Jan 09, 2012 at 06:04 PM 0
Share

This is what I want!! Its GREAT solution. Thank you very much!

avatar image Bunny83 · Jan 28, 2012 at 07:32 PM 0
Share

@GigaDeath: And this is not an answer! I've converted your "answer" into a comment.

avatar image AbsurdHart · Apr 20, 2012 at 12:22 AM 0
Share

@$$anonymous$$Pasek, Bunny83, Eric5h5: or anyone else:

Is there an equivalent method of doing this, but for Particle Effects, so that they Emit/Animate on Pause $$anonymous$$enus?

avatar image dreamychris · Feb 01, 2013 at 03:11 AM 0
Share

Thanks everyone, very helpful!

@AbsurdHart: To turn the above code into an equivalent solution for particle effects, you could try replacing line 34 above with "particleSystem.time = _accumTime", and removing other references to AnimationStates. I haven't tested this myself though. (And yeah, I realise your question is old :) )

avatar image kebrus · Apr 10, 2013 at 01:29 AM 0
Share

i was using this code to run an animation while paused, but i can't run animation events with this, they simply wont run, is there any way around this?

Show more comments
avatar image
40

Answer by Salazar · Aug 30, 2014 at 03:42 PM

I used Animator Update Mode --> UnscaledTime option in animator component of the gameobject.

For the solution of animated sprite menus.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baintastic · Mar 12, 2015 at 07:43 PM 1
Share

This should be the accepted answer. I just wasted way too much time on the current "accepted" answer.

avatar image psykojello2 · Mar 17, 2015 at 07:46 AM 1
Share

I know this is late, but one more thumbs up for this answer. It did exactly what I needed without any code.

avatar image lodendsg · May 23, 2015 at 12:14 PM 1
Share
  • for reading past the accepted answer ;) changing the Update $$anonymous$$ode on the animator gives the desired result with 0 code.

avatar image shivesh · Feb 26, 2016 at 06:24 AM 0
Share

@Salazar BRO YOU ARE GREAT !!

avatar image
3

Answer by Eric5h5 · Nov 03, 2010 at 10:24 PM

Set the timeScale for pausing to something like .00001 instead of 0, and when pausing or resuming set the animation speeds to "1.0/Time.timeScale".

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image equalsequals · Nov 03, 2010 at 11:45 PM 0
Share

Did you mean 1 divided by Time.timeScale or pausing/resu$$anonymous$$g respectively?

avatar image Eric5h5 · Nov 04, 2010 at 01:41 AM 0
Share

equalsequals: I meant one divided by Time.timeScale. Edited for clarity.

avatar image RichMakeGame · May 16, 2012 at 10:44 PM 0
Share

I just tried this method and it didn't work well at all for me. it worked ok until timescale reached about 0.4, after which there was a mismatch between the normal speed and the paused speed, growing more severe the slower time is set. At 0.0001 the animation played extremely slowly, despite being set to play at 10000 times speed

avatar image jorjdboss · Jan 25, 2014 at 09:50 AM 0
Share

the animation speed is a float and floats lose precision at high values such as 100000.

avatar image
1

Answer by Paulius-Liekis · Nov 03, 2010 at 10:22 PM

You just have to control animation time manually - it works with regular or Unity animations. Get the actual delta time from realtimeSinceStartup or something else that is not affected by time scale and set appropriate time on your animation.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image equalsequals · Nov 04, 2010 at 07:03 PM 0
Share

Thanks for the idea, it was a nudge in the right direction. I had assumed this was the route I needed to go, but I was looking for something with a little less rigmarole. I submitted my own answer with code as to how I solved this.

avatar image
1

Answer by Herman-Tulleken · Nov 04, 2010 at 06:19 AM

We use the following (clumsy, but workable) scheme. Most of our simulations run of a separate variable ourOwnDeltaTime, which is the same as Time.deltaTime when the game is running, but 0 when the game is paused. Things that should not pause runs off the proper deltaTime.

It is clumsy, because animations and particles have to be played and paused separately (because, of course, internally they still run of Time.deltaTime). It also requires a lot of discipline (you have to remember never to use the Time.deltaTime everywhere), and might not work well with 3rd party code.

A benefit is that game speed and interface animations can be controlled separately.

I could never understand why there is not a better built-in solution for pausing (in any game-engine I have worked with)...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image equalsequals · Nov 04, 2010 at 04:01 PM 0
Share

I agree, there should be something internal for this, because every solution I've found is nothing short of a hack. It's unfortunate that those are the only options here. Luckily there is extremely little 3rd party code implemented in this project, and anything that wasn't written in-house is not timescale-dependent. I can implement this custom deltaTime with little to no rework outside of a single class in our animation framework.

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pause Script Only Partially Works? (Gui Toggles, But Time.timeScale Does Not) 1 Answer

How to pause Time(Time.timeSinceLevelLoad) 1 Answer

Pause game while GUI instructions displayed? 1 Answer

How to pause a game? 2 Answers

Time.timeScale = 0; only pauses animations? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges