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.


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…