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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to achieve slow motion by timeline

Discussion in 'Cinemachine' started by liujunjie612, Dec 12, 2018.

  1. liujunjie612

    liujunjie612

    Joined:
    Oct 26, 2016
    Posts:
    2
    i dont want to use timescale to do it, how to complete it by timeline
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Try attaching this to the game object with the playable director playing timeline.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Playables;
    3.  
    4. [RequireComponent(typeof(PlayableDirector))]
    5. public class TimelineTimeScale : MonoBehaviour
    6. {
    7.     public float timeScale = 1.0f;
    8.  
    9.     void Awake()
    10.     {
    11.         var director = GetComponent<PlayableDirector>();
    12.         if (director != null)
    13.         {
    14.             director.played += SetSpeed;
    15.             SetSpeed(director); // in case play on awake is set
    16.         }
    17.     }
    18.  
    19.     void SetSpeed(PlayableDirector director)
    20.     {
    21.         if (director != null && director.playableGraph.IsValid())
    22.         {
    23.             director.playableGraph.GetRootPlayable(0).SetSpeed(timeScale);
    24.         }
    25.     }
    26.  
    27.     void OnValidate()
    28.     {
    29.         SetSpeed(GetComponent<PlayableDirector>());
    30.     }
    31. }
     
    Patoto64, Blockehh and whkona like this.
  3. liujunjie612

    liujunjie612

    Joined:
    Oct 26, 2016
    Posts:
    2
  4. Mori_X

    Mori_X

    Joined:
    Mar 17, 2023
    Posts:
    11
    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?
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,208
    Probably it only works in Play mode. Does it work when you play the game? (not just the timeline)
     
  6. Mori_X

    Mori_X

    Joined:
    Mar 17, 2023
    Posts:
    11
    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
     
  7. aurore-fiorini

    aurore-fiorini

    Unity Technologies

    Joined:
    Nov 23, 2020
    Posts:
    1
    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.
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Playables;
    4.  
    5. [RequireComponent(typeof(PlayableDirector))]
    6. public class TimelineTimeScale : MonoBehaviour
    7. {
    8.     public float timeScale = 1.0f;
    9.  
    10.     void Awake()
    11.     {
    12.         SetSpeed(GetComponent<PlayableDirector>()); // in case play on awake is set
    13.     }
    14.  
    15.     void OnDidApplyAnimationProperties()
    16.     {
    17.         SetSpeed(GetComponent<PlayableDirector>());
    18.     }
    19.  
    20.     void SetSpeed(PlayableDirector director)
    21.     {
    22.         if (director != null && director.playableGraph.IsValid() && director.state == PlayState.Playing)
    23.         {
    24.             director.playableGraph.GetRootPlayable(0).SetSpeed(timeScale);
    25.         }
    26.     }
    27. }
    28.  
     
    Last edited: Apr 21, 2023
    antoinecharton likes this.