How to achieve slow motion by timeline

i dont want to use timescale to do it, how to complete it by timeline

Try attaching this to the game object with the playable director playing timeline.

using UnityEngine;
using UnityEngine.Playables;

[RequireComponent(typeof(PlayableDirector))]
public class TimelineTimeScale : MonoBehaviour
{
    public float timeScale = 1.0f;

    void Awake()
    {
        var director = GetComponent<PlayableDirector>();
        if (director != null)
        {
            director.played += SetSpeed;
            SetSpeed(director); // in case play on awake is set
        }
    }

    void SetSpeed(PlayableDirector director)
    {
        if (director != null && director.playableGraph.IsValid())
        {
            director.playableGraph.GetRootPlayable(0).SetSpeed(timeScale);
        }
    }

    void OnValidate()
    {
        SetSpeed(GetComponent<PlayableDirector>());
    }
}
3 Likes

Thanks

Hi,

I have troubles getting this one to work. I attached the script directly to the timeline and tried to animate it, to have several moments going faster and slower. Hitting the play button doesn’t show any changes with the time. Working with Unity 2021.3.16f1

Anything I got wrong?

Probably it only works in Play mode. Does it work when you play the game? (not just the timeline)

Sorry, my mistake. I meant I tested it in runtime, yes, not in editor mode! Doesn’t get slower. Script might be outdated? I am not a programmer, so xD

Otherwise one way to do it without programming is to nest the timeline you want to slow down within a parent Timeline
https://docs.unity3d.com/Packages/com.unity.timeline@1.8/manual/wf_nested.html
and then set the speed of the control clip directly in the parent timeline and play the parent.
This will allow you to play your timeline in slow motion

Edit: The script is working, but it’s setting the speed only when the Timeline starts playing so if you’re animating the value it won’t impact the Timeline. the solution above will not work for animation either as the Speed will be set only once.
As an alternative you could use this modified version of the script and then animate the timeScale value.

using System;
using UnityEngine;
using UnityEngine.Playables;

[RequireComponent(typeof(PlayableDirector))]
public class TimelineTimeScale : MonoBehaviour
{
    public float timeScale = 1.0f;

    void Awake()
    {
        SetSpeed(GetComponent<PlayableDirector>()); // in case play on awake is set
    }

    void OnDidApplyAnimationProperties()
    {
        SetSpeed(GetComponent<PlayableDirector>());
    }

    void SetSpeed(PlayableDirector director)
    {
        if (director != null && director.playableGraph.IsValid() && director.state == PlayState.Playing)
        {
            director.playableGraph.GetRootPlayable(0).SetSpeed(timeScale);
        }
    }
}
2 Likes