How To Reset/Restart A Unity Timeline
Today I want to quickly show how you can reset or restart a Timeline, since there doesn’t seem to be an official method for it yet.
Today’s Objective: Reset and restart a timeline mid-play using code, so that it restarts whenever you die to the dog.
When the player passes a certain invisible trigger collider, the timeline is started and the dog spawns and runs to the right after a short delay. If the dog touches the player, the player dies and is respawned prior to the timeline starting.
Since Timelines/Playable Director’s currently have no in-built method to reset them, without making a solution myself, I have 2 options.
I can either keep the timeline playing, or “Stop” it which is actually just a pause, leaving the objects in place. Neither of these suit my needs and would not work for my game.
What I want is for the timeline (and therefore, the dog) to be reset on death, so that it vanishes until I pass the trigger again, restarting it.
The code for this is pretty simple, comprising of 3 commands to the PlayableDirector component:
Note: Some of you may have noticed one problem which still remains in this solution.
While the dog (and all other timeline objects) are being reset and restarted correctly, there is a split-second when the Timeline restarts where the objects (like the dog) are still in their previous position from the last Timeline play.
I haven’t yet found a solution to fix this, but I haven’t been working on it long, so I will update this post if I find a solution.