Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Reset timeline from Code?

Discussion in 'Timeline' started by DavidSmit, Nov 10, 2017.

  1. DavidSmit

    DavidSmit

    Joined:
    Apr 1, 2014
    Posts:
    50
    Hi, I've been trying to figure out how to properly reset a timeline that's half-way through when a player hits the reset button.
    At the moment I stop the playable director and set the time to 0. However the objects in the timeline, including particle effects that are being controlled by the Control track, just stay in their current position.
    Which is weird on it's own, but when a player then triggers it again, everything, including the particle effects shoot back to the starting position, leaving a trail. Which is not desirable.

    Am I missing a simple Api call that cleanly resets this?

    Thanks!
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    You can use Activation and PostPlaybackState on the Control Track (which would disable the particle system), but otherwise this looks like a bug.
     
    DavidSmit likes this.
  3. DavidSmit

    DavidSmit

    Joined:
    Apr 1, 2014
    Posts:
    50
    I've run into this again unfortunately.
    It's actually not only the particles, but anything set by the playable director, that doesn't get properly reset.
    I have an area which gets closed off by a activation track in a Timeline. But after the player crosses it, and restarts the level, I reset the Timeline/PlayableDirector by Stopping it, and setting the time to 0. However the objects stay active and in their last position. The camera that is triggered in that track also stays active.

    The only way to restart it, is to trigger the PlayableDirector again.
    Perhaps something to do with destroying the playableGraph that's behind it?

    I've updated to 2017.3, and the same issues occurs.

    A ResetToStartConditions or similar method would be great.
     
  4. MartinG

    MartinG

    Joined:
    Apr 17, 2013
    Posts:
    16
    We have the same problem.

    It´s not about particles in timeline. If there are particles or not, a timeline updates only the controlled objects, when the timeline is playing. That means, if I say "playableDirector.Stop()" in script. The timeline stops. BUT, the timeline should also jump back to the first frame. That´s the way a "stop" has to work. Maybe an "old" example, but hit stop on a CD Player, resets the player immediately to beginning of the CD. Pause a CD Player holds the position and on Play() the player resumes. Timeline Stop() resets the playableDirector, but the "Frame Update" will only be visible, when Play() is called again. But again, this is not what a user wanted to have when he says "stop()". If I set time = 0 and say then stop(), it´s the same. The Animations freeze, but some times later, when I call play again, they are realy resetted on screen. But this should happen immediately on "stop()". Or in other words, the timeline current Frame should not only be updated when in PlayMode, but also in Stop-Mode. May be just one time and not everytime.

    Greetings,

    Martin
     
    DavidSmit likes this.
  5. Rodmanix

    Rodmanix

    Joined:
    Dec 4, 2012
    Posts:
    14
    Oh wow, I've spent about 3 days running through my code to try and figure this out. My issue is that the second time I play the timeline, any script that I want to activate gets ignored. So we can't play a timeline that has already been played. Ouch!
     
  6. andrews_unity

    andrews_unity

    Unity Technologies

    Joined:
    Dec 11, 2015
    Posts:
    264
    So for Folks running into this issue :D you can just do this:

    Code (CSharp):
    1. playableDirector.time = 0;
    2. playableDirector.Stop();
    3. playableDirector.Evaluate();
    This will force the playable to Evaluate at time 0 and reset everything.
     
    JDoe1, Fenikkel, olleyOop and 9 others like this.
  7. Joybutton

    Joybutton

    Joined:
    Jun 25, 2014
    Posts:
    4
    Would it be possible to share the whole code that achieves this? I've been trying for a while and can't quite get it to work.

    I have a timeline that I need to be able to stop and reset to zero.

    "At the moment I stop the playable director and set the time to 0" How can this be accomplished?

    Edit: 'Current Time' is only accessible when the scene is active so it can't be added as a timeline event as far as I can tell. So I need a small script I can trigger that accomplishes the same thing, resets 'Current Time' to 0
     
  8. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Hi, i'm new to timelines and generaly to unity but what about this
    Code (CSharp):
    1. pDir.initialTime = 0f;
    2. pDir.time = 0f;
    personaly i use this to start a cutscene at a further time (using values > 0 ... ;)) if the player wants to restart the race.
    that works prety good but is it right ?

    nvm, finaly
    Code (CSharp):
    1. pDir.time = t;
    seams sufficient to do the job. that start the timeline from t seconds.
    I just would know if it was a good way to do that...
     
    Last edited: Jul 12, 2018
  9. TheWarper

    TheWarper

    Joined:
    Nov 3, 2013
    Posts:
    112
    I just deactivate and reactivate the timeline game object and it properly resets for me.

    If there is a chance that it's already active and I want to replay it then I just use this code:

    Code (CSharp):
    1. myTimeline.SetActive(true);
    2. PlayableDirector playableDirector = myTimeline.GetComponent<PlayableDirector>();
    3. playableDirector.time = 0;
    4. playableDirector.Play();
    Although, perhaps this is a bit more clear and not redundant:

    Code (CSharp):
    1. if(!myTimeline.activeSelf)
    2.   myTimeline.SetActive(true);
    3. else
    4. {
    5.   PlayableDirector playableDirector = myTimeline.GetComponent<PlayableDirector>();
    6.   playableDirector.time = 0;
    7.   playableDirector.Play();
    8. }
     
    Last edited: Aug 25, 2018
  10. alexanderFabula

    alexanderFabula

    Joined:
    Sep 1, 2019
    Posts:
    22
    Hey

    I have a problem with resetting the timeline while having a cinemachine track with a clip.

    I reset all my timelines based on my checkpoint system.


    I use the solution described above. I use director.Stop(); then director.time = 0f (or duration), then director.Evaluate();

    One of my timelines sets a virtual camera live even though it's not highest priority? If I move the clip on the track away from the beginning, it does not do it. So it seems like a weird bug or maybe my method of using Stop, set time, evaluate is not correct.
     
  11. alexanderFabula

    alexanderFabula

    Joined:
    Sep 1, 2019
    Posts:
    22
    The same happens with the rotation of the character object. None of the timelines are playing but it seems like they are so I cannot rotate the character object and cinemachine get stuck in a specific virtual camera.