Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Timeline change animation playback speed problem

Discussion in 'Timeline' started by vamky, Jul 26, 2017.

  1. vamky

    vamky

    Joined:
    Aug 9, 2014
    Posts:
    70
    Hi

    I want to change the playback speed of target animator in runtime because timescale changes the speed of everything. (So I cannot use the time dilation track in the default playable package).

    I used timeline playable wizard to create a custom track of my own to alter the target animator speed. However although the script worked, the playback speed did not change. Is there anything wrong with my script? Or is there any work around on this issue?

    I also tried using Chronos's lock clock function but Chronos doesn't seem to work in timeline either.

    here is my code:

    Code (CSharp):
    1.  
    2.  
    3. public class AnimatorPlaySpeedMixerBehaviour : PlayableBehaviour
    4. {
    5.     float m_DefaultSpeed;
    6.  
    7.     Animator m_TrackBinding;
    8.     bool m_FirstFrameHappend;
    9.  
    10.     public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    11.     {
    12.         m_TrackBinding = playerData as Animator;
    13.  
    14.         if (!m_TrackBinding)
    15.             return;
    16.  
    17.         if (!m_FirstFrameHappend)
    18.         {
    19.             m_DefaultSpeed = m_TrackBinding.speed;
    20.             m_FirstFrameHappend = true;
    21.         }
    22.  
    23.         int inputCount = playable.GetInputCount ();
    24.  
    25.         float blendedSpeed = 0f;
    26.         float totalWeight = 0f;
    27.         float greatestWeight = 0f;
    28.         int currentInputs = 0;
    29.  
    30.  
    31.         for (int i = 0; i < inputCount; i++)
    32.         {
    33.             float inputWeight = playable.GetInputWeight(i);
    34.             ScriptPlayable<AnimatorPlaySpeedBehaviour> inputPlayable = (ScriptPlayable<AnimatorPlaySpeedBehaviour>)playable.GetInput(i);
    35.             AnimatorPlaySpeedBehaviour input = inputPlayable.GetBehaviour ();
    36.  
    37.             blendedSpeed += input.Speed * inputWeight;
    38.             totalWeight += inputWeight;
    39.  
    40.  
    41.             if (inputWeight > greatestWeight)
    42.             {
    43.                 greatestWeight = inputWeight;
    44.             }
    45.  
    46.             if (!Mathf.Approximately (inputWeight, 0f))
    47.                 currentInputs++;
    48.          
    49.         }
    50.  
    51.         m_TrackBinding.speed = blendedSpeed + m_DefaultSpeed * (1f - totalWeight);
    52.         if (currentInputs != 1 && 1f - totalWeight > greatestWeight)
    53.         {
    54.         }
    55.     }
    56.     public override void OnGraphStop(Playable playable)
    57.     {
    58.         m_TrackBinding.speed = m_DefaultSpeed;
    59.         m_FirstFrameHappend = false;
    60.     }
    61.  
    62. }
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Is the target animator playing back a state machine or a timeline track? If it's part of the timeline, the playback speed of the animator will have no effect.
     
  3. vamky

    vamky

    Joined:
    Aug 9, 2014
    Posts:
    70
    Yes it is part of the timeline, is there any way to change the playback speed of a timeline animation clip then? I think this would be a fairly common need when working with timeline. Time dilation in the default playable package slows everything including UI items, which is not what I want to achieve.
     
  4. taylank

    taylank

    Joined:
    Nov 3, 2012
    Posts:
    182
    Is there any update on this? I have a similar requirement. I have a bunch of nested timeline assets, with a master timeline driving a bunch of other timelines. I want to slow down the animation in a child timeline while the master keeps running at the same speed. Global time dilation doesn't help.
     
  5. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Both control track clips and animation track clips allow you to change the playback speed (Speed Multiplier) per clip.
     
  6. taylank

    taylank

    Joined:
    Nov 3, 2012
    Posts:
    182
    Sorry I wasn't clear about what I meant. Yes you can change animation speed per clip, but then the value is fixed for the clip. What I need is a way that works like time dilation but is not global: e.g. reduce the speed of this animation to .5 for 10 frames, then continue playing it at speed of 1 for the remainder of its duration
     
  7. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    No, there is nothing like that. The closest would be to _split_ the clip into multiple clips, then modify the speed per clip - and use ripple mode as the speed modifier will change the length of the clip accordingly and push/pull other clips as necessary. The change in speed would be instantaneous and not gradual, but it may suit your needs.

    Alternatively you might be able to generate a new animation clip by using the Unity Recorder (which can record a gameobject during playmode) combined with a timeline with a single animation clip and a time dilation track.

    Or just duplicate the animation clip, and modify the copy by stretching and compressing keyframes in the animation window.