1
\$\begingroup\$

I want to setup a way that the timeline will pause at a particular time/frame. I have tried this, and it doesn't work:

public PlayableDirector playableDirector;
void Update() {
    if (playableDirector.time == 4f)
        playableDirector.pause();
}

Also, is it alright to leave this in the update method?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You're testing whether the time is exactly 4.00000 seconds. Do you not want to pause if the time is 3.99999 or 4.00001 ? \$\endgroup\$
    – DMGregory
    Commented Jun 11, 2019 at 15:45

1 Answer 1

1
\$\begingroup\$

The variable playableDirector.time is rarely going to be exactly 4.00000f. It's a floting point value, so it will usually be slightly off. Your update might get called at time 3.99215f and then again at time 4.01721f.

That's why you should generally never check float values for equality. You should only check them for being larger or smaller than another value.

What you can do instead is check if the time just passed the 4f second mark. In order to do that you need to keep track of the previous time.

public PlayableDirector playableDirector;
private float lastTime;

void Update() {
    if (playableDirector.time >= 4f && lastTime < 4f ) {              
         playableDirector.pause();
    }
    lastTime = playableDirector.time
}

There are two edge-case this code won't handle: If your timeline is looping and your break-point is very close to the beginning or very close to the end of the timeline, this code might not work reliably because playableDirector.time will be smaller than lastTime during the first update of each loop. If these two edge-case are relevant for your particular case, then this slightly more complicated code should handle it:

public PlayableDirector playableDirector;
private float lastTime;

void Update() {
    if ( (playableDirector.time >= 4f && lastTime < 4f ) || // usual case
         (lastTime > playableDirector.time && lastTime < 4f ) || // breakpoint was after last update in timeline            
         (lastTime > playableDirector.time && playableDirector.time >= 4f) ) // breakpoint before first update in timeline       
    {              
         playableDirector.pause();
    }
    lastTime = playableDirector.time
}
\$\endgroup\$
1
  • \$\begingroup\$ I did not know that float values can skip a few decimal points by frame intervals...Thank you. \$\endgroup\$ Commented Jun 11, 2019 at 16:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .