- Home /
How to do time reversal with Unity2D / Physics2D?
Hi guys, could you share your thought about how to do time reversal in Unity2D? I mean, is it possible to do it with Physics2D, and if it's not possible, what options are there? Giving up physics and using some tweener to implement reverse animation?
Do you want to make reverse mechanim animation or physics 2D reverse effects (i.e. reverse gravity etc)?
Yes, I'd like to mimic the effect of the Braid game where you can reverse all the objects' move backward, while using physics2d to control objects. Is there any difference/optimization in doing it in 3d or 2d?
Sadly it is not possible to reverse Time.timeScale, so the only way I could think is using a Playback (or replay) script and play the events in reverse order. $$anonymous$$aybe something like http://u3d.as/content/soft-rare/ez-replay-manager/1QY or if you wish to make your own, http://www.m2h.nl/files/replay/replay.pdf is a good reference
Answer by fafase · Apr 13, 2014 at 07:12 AM
Here are a few thoughts.
I consider you can only replay x amount of seconds, not the whole game.
Create a manager that holds a list of object. This list implements IComparable with a Compare method that will check each object for time and activity. So for all objects it checks if it is still alive, if so, do nothing, if dead, has it been dead for more than x seconds, if so remove from the list. This allows you to revive object in your backward process that were dead recently.
If you only replay Transform then you only store your transforms in a queue. The point of a queue here is that you place the most recent at the end and remove the oldest one at the front. You then only need to play backward. If you need to record other happening, then you could use your queue but storing object of a class you defined. The class qould have Transform and other states you need to keep track.
As you go backward in your queue, first you stop recording and second you read the info. For transform you simply need to interpolate from current to next (which is actually previous), as you get to next info, you check if there has been a record for other modification, like color or it got destroyed/deactivated so now you need to reactivate it.
In order to avoid all kind of check for each object, you could have them inheriting a top class with an abstract method in which for each Subclass you implement in the proper way. Then in your backward class you just call that method for all objects regardless what it does in the implementation.
Those are just thoughts but maybe that will give some directions.