Currently I’m able to align root motion between clips by apply ‘match offsets to previous clip’ to each clip, but if I play the whole track again, the start position is reset.
I want the track start from the position where last track ends. I tried to implement a playable which modify animation track offsets after the track ends, but unfortunately I can’t find a way to get the animation track reference.
Is there any way to align root motion between each track play? Thanks.
Hi,
Timeline doesn’t have a way to do this out of the box, although it is an option we have recently been discussing. One way to do this is using the track offsets on the animation track. Here’s an example monobehaviour you could try that automatically sets the track offsets to make the timeline play relative to the current positions of the animated objects.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
[RequireComponent(typeof(PlayableDirector))]
public class PlayTimelineRelative : MonoBehaviour
{
private PlayableDirector director;
void Awake()
{
director = GetComponent<PlayableDirector>();
}
void OnEnable()
{
// if the director is playing, timeline changes won't have an effect
director.Stop();
var timelineAsset = director.playableAsset as TimelineAsset;
if (timelineAsset == null)
return;
foreach (var track in timelineAsset.GetOutputTracks())
{
var animTrack = track as AnimationTrack;
// could check for a specifically named track here as well...
if (animTrack == null)
continue;
var binding = director.GetGenericBinding(animTrack);
if (binding == null)
continue;
// the binding can be an animator or game object with an animator
var animator = binding as Animator;
var gameObject = binding as GameObject;
if (animator == null && gameObject != null)
animator = gameObject.GetComponent<Animator>();
if (animator == null)
continue;
// turn on the track offsets, and apply the current position
animTrack.applyOffsets = true;
animTrack.position = animator.transform.position;
animTrack.rotation = animator.transform.rotation;
}
// start the director. This will build the graph with embedded offsets
director.Play();
}
public void OnDisable()
{
if (director == null)
return;
// turn off the offsets so the editor resets to 0,0,0
var timelineAsset = director.playableAsset as TimelineAsset;
if (timelineAsset == null)
return;
foreach (var track in timelineAsset.GetOutputTracks())
{
var animTrack = track as AnimationTrack;
if (animTrack == null)
continue;
animTrack.applyOffsets = false;
}
}
}
Thank you for your reply! I test the code just now and it just works as expected!
And if the binding object is not under the scene root, use ‘localPosition’ and ‘localRotation’ instead of ‘position’ and ‘rotation’
Yes, right. You can also just move the parent instead of using the script since Timeline only animates the local position.
Hi there,
I’m attempting to do this currently with trackOffset since applyOffsets has been deprecated. It doesn’t seem to change anything at runtime. Do I need to force rebuild the graph or something? The director is stopped beforehand and played afterwards as in the sample code above. If I inspect the track in the editor while playing, the value seems set appropriately - it just doesn’t actually apply the offset (it works in edit mode if I manually modify the offset on the AnimationTrack).
Also does it work with several Playable directors wanting different relative offset yet playing the same Timeline asset at a single time ?