Search Unity

Bug Character offset issue when Timeline GameObject is not selected (100% reproducible)

Discussion in 'Timeline' started by luispedrofonseca, Mar 27, 2023.

  1. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    Hello, I'm running into a very weird bug where a character position is being offset during an animation blend. It's as if both animations positions are being added instead of blended. This only happens before selecting the Timeline GameObject for the first time, after play starts.
    Important to note that I'm creating the track and adding the clips to it at runtime.

    Here's a video of the issue:


    And here's the code that reproduces the issue:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Playables;
    4. using UnityEngine.Timeline;
    5.  
    6. public class TimelineTest : MonoBehaviour
    7. {
    8.     [SerializeField] private PlayableDirector _playableDirector;
    9.     [SerializeField] private Animator _animator;
    10.     [SerializeField] private AnimationClip _animationClip1;
    11.     [SerializeField] private AnimationClip _animationClip2;
    12.  
    13.     private TimelineAsset _timelineAsset;
    14.     private AnimationTrack _animationTrack;
    15.  
    16.     private void Start()
    17.     {
    18.         // Create TimelineAsset
    19.         _timelineAsset = ScriptableObject.CreateInstance<TimelineAsset>();
    20.         _playableDirector.playableAsset = _timelineAsset;
    21.  
    22.         // Create AnimationTrack and add it to the timeline
    23.         _animationTrack = _timelineAsset.CreateTrack<AnimationTrack>(null, "Animation Track");
    24.         _animationTrack.trackOffset = TrackOffset.ApplySceneOffsets;
    25.  
    26.         // Bind the animation track to the animator
    27.         _playableDirector.SetGenericBinding(_animationTrack, _animator);
    28.  
    29.         // Create animation clip 1
    30.         var timelineClip = _animationTrack.CreateClip(_animationClip1);
    31.         timelineClip.start = 1;
    32.  
    33.         // Create animation clip 2
    34.         timelineClip = _animationTrack.CreateClip(_animationClip2);
    35.         timelineClip.start = 4f;
    36.  
    37.         // Rebuild graph and play
    38.         _playableDirector.RebuildGraph();
    39.         _playableDirector.Play();
    40.     }
    41. }
    42.  
    And here's the repro project with the code you see above: https://www.icloud.com/iclouddrive/03dboZpFKXEqVI0DZtVbaxAkg#TimelineTest

    Any help would be greatly appreciated!

    P.S.: Forgot to mention that this happens on every Unity version I tested (2021, 2022 and 2023) and Timeline version as well (1.4 up to 1.8.2)
     
    Last edited: Mar 27, 2023
  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    126
    I'm not sure if this is the reason, but
    CreateClip()
    won't set the
    TimelineClip.blendInDuration / blendOutDuration
    for you, so you have to calculate the overlap time and set the blend time by yourself.
    (The Timeline Editor friendly updates that for you, so you would start get proper blending when you first open the Editor)
     
    Last edited: Mar 28, 2023
  3. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    177
    That is correct; when creating clips manually, the blend values have to be calculated manually too if there is an overlap. In the project, the clips have an overlap of about 0.8 seconds but the blend values are not set, so that explains the incorrect result. Here's how to set the blend values:

    Code (CSharp):
    1. // Create animation clip 1
    2. var timelineClip = _animationTrack.CreateClip(_animationClip1);
    3. timelineClip.start = 1;
    4.        
    5. // Create animation clip 2
    6. var timelineClip2 = _animationTrack.CreateClip(_animationClip2);
    7. timelineClip2.start = 4f;
    8.  
    9. // Calculate clip overlap and set blend values
    10. var overlapDuration = Math.Max(0d, timelineClip.end - timelineClip2.start);
    11. timelineClip.blendOutDuration = overlapDuration;
    12. timelineClip2.blendInDuration = overlapDuration;
     
    akent99 and tsukimi like this.
  4. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    Thanks @Yuchen_Chang and @julienb! That fixed it!

    I couldn't find this information anywhere in the documentation and with the Editor behaviour shown in my recording, it really made it look like a bug.
     
    tsukimi likes this.