- Home /
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!
@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.
@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.
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
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...
Your answer
Follow this Question
Related Questions
How do I animate graphics in a custom EditorWindow ? 0 Answers
How to play animation just once whenver i shoot a bullet( bullet's flash animation) ? 0 Answers
Distribute terrain in zones 3 Answers
Trying to manage an Animation. AnimationState.time doesn't adjust the animation 1 Answer
How to use addforce after animation? 1 Answer