• 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
Question by Coguelin · Jan 14, 2013 at 02:15 PM · c#animationtimeanimationstatereplay

C# Scrubbing back through an Animation in script

Hey folks,

I'm in the rather elaborate process of building a system to rewind the state of my game "in real time" and I've hit an awkward issue : I can rewind the state of objects just fine (each rewindable object simply keeps a list of structs containing the necessary variable e.g. position, rotation, behaviour state, animation clip, animation time, etc.) but rewinding the animations themselves just doesn't look right.

In short :

I have a time manager that broadcasts a record or rewind event n times per second, to which rewindable objects listen and respond by dutifully recording or reverting their state.

In the case of animated objects (which all objects will of course eventually be on some level) I thought the best thing to do would be to create a coroutine called every "rewind" event. It looks a little like this :

 IEnumerator PlayBack(float previousTime, float targetTime, float deltaTime, AnimationState state)
     {
         playBack = false;
 
         float i = 0f;
         
         float rate = 1/deltaTime;
         
         state.speed = -myTimeManager.TockToTickRatio; // This is the ratio of rewind "tocks" to record "ticks" which would (for e.g.) be 1 if the record frequency is the same as the rewind frequency.
 
         state.time = previousTime;
 
         state.weight = 1.0f;
 
         state.wrapMode = WrapMode.Default;
 
         state.enabled = true;
         
         while(i < 1f && !playBack)
         {
             i += Time.deltaTime * rate;
             
             state.time = Mathf.Lerp(previousTime, targetTime, i);
             
             yield return null;
         }
     }

But my animation plays incredibly jerkily, and I suspect that I am either using the wrong approach or missing a vital piece of information about how Unity animations work.

NB: In this case "deltaTime" is the rate of rewind events per second. So that we're essentially lerping from the previous animation time to the target animation time over the time between rewind events.

Does anyone have any experience of this. I suspect this is something that anyone trying to implement a time-rewinding mechanic will have encountered.

Thanks!

Comment

People who like this

0 Show 3
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 Coguelin · Jan 14, 2013 at 06:09 PM 0
Share

@PAEvenson Thanks for your help! I've tried a number of constellations but I always get more or less the same result : a jerky stop-motion-esque rewinded animation. I suspect you're probably right, but I'm doing something else wrong somehow. If I set the speed to 0f and try to scrub through the animation by simply lerping the time backwards, I still end up with roughly the same result.

The rewinded animation basically looks quite choppy, speeding up and slowing down strangely. This might be related to my previousTime variable (if it lerps inconsistent lengths of time, for example) since previousTime is essentially the animation's time at the start of the coroutine (since my post, I've switched over to normalised time).

Part of my concern is that, once this mechanic works to my satisfaction, I'd like to handle reversing cross-fades by lerping previous- and target-weight as well the the respective animation times, which is likely to compound my problem to no end.

As an addendum : Does anyone know of example projects which implement a rewinding mechanic? I have the horrible feeling this might be quite a simple/stupid oversight.

avatar image Coguelin · Feb 01, 2013 at 01:57 PM 0
Share

@PAEvenson I keep meaning to reply and getting sidetracked by a-thousand-and-one different things (it turns out building a rewind/replay system is haaard) but I rebuilt my animations manager to work purely with animation speed, recording start and end times to make sure I can properly resume interrupted animations* and it works wonderfully.

All this to say "you were right, thanks for your help!" R.

*We're not quite there yet, but at least I'm fighting new demons.

avatar image PAEvenson · Feb 01, 2013 at 03:04 PM 0
Share

Ha! I am in the same boat. I didnt even see you first response. Glad it helped you. Ill convert my comment to an answer for people looking at this in the future :P

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by PAEvenson · Jan 14, 2013 at 04:57 PM

From the looks of things, you are setting the speed, then manually changing the time of the animation. I suggest doing one or the other. Right now they are stepping on each others toes. Both are manipulating the animation. The speed is the playback rate of the animation. Hope this helps...

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to play animation just once whenver i shoot a bullet( bullet's flash animation) ? 0 Answers

How do I animate graphics in a custom EditorWindow ? 0 Answers

How to use addforce after animation? 1 Answer


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