Coder Social home page Coder Social logo

unitytimerewinder's Introduction

Time Rewinder for Unity

Rewind time in Unity with ease! While there are certain frameworks that let you rewind time in Unity, they are usually quite restrictive and hard to modify. I faced these problems myself when i needed to add time rewind mechanics in my game and none of the solutions that i have found were good enough. That is why i decided to start this open source project that i hope you find usefull. Time Rewinder uses highly efficient circular buffer implementation to store and retrieve the rewinded values.

Customizability is one of the main points of this project, so it can be used in any of your custom Unity projects and you can track and rewind anything you want!

Straight from the box you can start rewinding Object active states, Transforms (position, rotation, scale), velocities, animators (also all animator layers and parameters), audios and particle systems.

How to install

You have two options how to install it into your project.

  • First option is to download prepared Unity package in Github release section. After download, open your Unity project and simply open the downloaded package, import dialogue window should appear (Unity should automatically associate the package with itself). If it didnt work out for you, you can also import it thru Unity package manager.
  • Second option is to download this sample project from Github and start using it, or just import the TimeRewinder folder under Assets/TimeRewinder into your project

Note: Unity versions 2019+ are officially supported

Features

TimeRewinder supports two types of rewinds.

  • Instant rewinds where you rewind time by specified amount of seconds
  • Rewinds with previews

The latter of the two is definitely more interesting option, cause you can freely choose which snapshot you can return to after you spectate these previews for yourself. This is especially helpfull if you want to give more control to player with rewinding time. As shown in demo-scene examples, player can choose on the time axis the exact moment he wants to return to, while the tracked attributes are paused. The showcase of this mechanic is shown right below. I think, this is also unique feature of this project, because i havent found a similar functionality in other Time Rewind frameworks.

ezgif com-gif-maker (9)

The classic functionality to rewind time by holding button, which you probably already know from other solutions is also here. These two types of rewinds inputs are prepared straight from the box, but it would be very easy to design completely new rewind input system. You would only have to call corresponding methods, that are all prepared and documented for you.

ezgif com-gif-maker (10)

How to use

Detailed steps how to use TimeRewinder are described in documentation and all important parts of code are also documented.

If you still face any problem, feel free to contact me, i can help you out.

Showcase of rewinding

In demo-scenes there are few examples of time rewinding, as well as two examples of tracking and rewinding custom variables. I recommend you look into it, so you get the idea how everything is connected. Here comes few other videos showcasing rewinding time from demo scenes

Particle_Audio.rewind.mp4
Custom_Object_Scale_Rewind.mp4
DynamicProjectilesRewind.mp4
Dinosaur_Animator_Rewind.mp4

And here is example and maybe motivation how it could look in actual game (mobile game), where you customize it for your needs :)

Time_Rewind_Promo.mp4

Coverage

Here you can watch nice video by SpeedTutor covering this asset

Watch the video

Shameless plug

The main propeler of this project is my released game where you can find this rewind system in practice. This physics based puzzle game is available for free on Google Play.

GooglePlay

Thank you if you decide to try it out :)

unitytimerewinder's People

Contributors

sitronx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unitytimerewinder's Issues

Angular velocity also needs to be tracked

when i tryed your project, I found someting weird.

spin_get_faster
Every time I rewind the time, it seemed that the cube is spinning faster and faster.

more_significant
I tested it further by adding another object. When I rotated the times, the objects did not return to their original angle.

Later I found that this project does not track angular velocity. I think that was the cause.

So I changed it to track the angular velocity as well, mimicking CircularBuffer trackedVelocities in RewindAbstracts.cs
this is what i add in RewindAbstracts.cs, Line 116

` #region AngularVelocity
CircularBuffer trackedAngularVelocities;
///


/// Call this method in Track() if you want to track angular velocity of Rigidbody
///

protected void TrackAngularVelocity()
{

    if (body != null)
    {
        trackedAngularVelocities.WriteLastValue(body.angularVelocity);            
    }
    else if (body2!=null)
    {
        trackedAngularVelocities.WriteLastValue(new Vector3(0, 0, body2.angularVelocity));
    }
    else
    {
        Debug.LogError("Cannot find Rigidbody on the object, while TrackAngularVelocity() is being called!!!");
    }
}
/// <summary>
/// Call this method in GetSnapshotFromSavedValues() to angular velocity of Rigidbody
/// </summary>
protected void RestoreAngularVelocity(float seconds)
{   
    if(body!=null)
    {
        body.angularVelocity = trackedVelocities.ReadFromBuffer(seconds);
    }
    else
    {
        body2.angularVelocity = trackedVelocities.ReadFromBuffer(seconds).z;
    }
}
#endregion`

and applied this in GenericRewind.cs
`public class GenericRewind : RewindAbstract
{
[SerializeField] bool trackPositionRotation;
[SerializeField] bool trackVelocity;
[SerializeField] bool trackAngularVelocity;
[SerializeField] bool trackAnimator;
[SerializeField] bool trackAudio;
[SerializeField] bool trackParticles;

[Tooltip("Fill particle settings only if you check Track Particles")]
[SerializeField] ParticlesSetting particleSettings;

protected override void Rewind(float seconds)
{

    if (trackPositionRotation)
        RestorePositionAndRotation(seconds);
    if (trackVelocity)
        RestoreVelocity(seconds);
     if (trackAngularVelocity)
        RestoreAngularVelocity(seconds);
    if (trackAnimator)
        RestoreAnimator(seconds);
    if (trackParticles)
        RestoreParticles(seconds);
    if(trackAudio)
        RestoreAudio(seconds);
}

protected override void Track()
{
    if (trackPositionRotation)
        TrackPositionAndRotation();
    if (trackVelocity)
        TrackVelocity();
    if (trackAngularVelocity)
        TrackAngularVelocity();
    if (trackAnimator)
        TrackAnimator();
    if (trackParticles)
        TrackParticles();
    if (trackAudio)
        TrackAudio();
}
private void Start()
{
    InitializeParticles(particleSettings);
}

}`

A way to make all this smoother

I switched FixedUpdate to Update as well as +=unscaledDeltaTime and set Time.timeScale = 0 when rewinding and back to 1 after because my falling objects were still falling while rewinding.

How to pause the game without breaking the time rewind?

I found information in the documentation that changing Fixed TimeStep or timeScale will lead to a lot of problems, but then how to pause the game? Is it possible to temporarily disable time tracking? Or do something else so that there are no problems with this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.