'Unity3D PlayableDirector play timeline animation backwards
I have made a zooming animation to get a close view of a GameObject. I am triggering this animation by using the PlayableDirector. I can just call the Play() method on this.
But if the user is zoomed in, I want to make it possible to zoom out as well. This should be able, but I do not know how. Is there something like a reverse method available on the PlayableDirector?
I would appreciate any insight / advice.
Thanks in forward.
Solution 1:[1]
At the moment, this is not possible in a simple way. The tricky way is to invert the clips in a semi-automatic way, as described here: https://forum.unity.com/threads/how-can-i-play-an-animation-backwards.498287/#post-5004851
Solution 2:[2]
you can use a while loop to evaluate the time
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class TimelineManager : MonoBehaviour
{
private PlayableDirector timeline;
private bool _isPlaying;
private void Awake()
{
timeline = GetComponent<PlayableDirector>();
}
public void PlayAnimation()
{
if(_isPlaying) return;
StartCoroutine(Play());
}
public void ReverseAnimation()
{
if(_isPlaying) return;
StartCoroutine(Reverse());
}
IEnumerator Play()
{
_isPlaying = true;
float dt = 0;
while (dt < timeline.duration)
{
dt += Time.deltaTime / (float) timeline.duration;
timeline.time = Mathf.Max(dt, 0);
timeline.Evaluate();
yield return null;
}
_isPlaying = false;
}
private IEnumerator Reverse()
{
_isPlaying = true;
float dt = (float) timeline.duration;
while (dt > 0)
{
dt -= Time.deltaTime / (float) timeline.duration;
timeline.time = Mathf.Max(dt, 0);
timeline.Evaluate();
yield return null;
}
_isPlaying = false;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Petr Varyagin |
| Solution 2 | Seyed Morteza Kamali |
