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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How can I access an Animation Playable Asset from script

Discussion in 'Timeline' started by fwalker, Jun 18, 2023.

  1. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    I am using scripts to add animation clips to tracks in the timeline. But I also need to be able to set the clip transform offsets on the clips. How would I go about accessing the Animation Playable Asset?
    Here is the code I use to create the tracks and add the clips what am I missing? I know I am close.

    public void CreateAnimationTracks()
    {
    _animationTrack = _timeline.CreateTrack<AnimationTrack>(null, "Track1");
    playableDirector.SetGenericBinding(_animationTrack, fighter1);

    TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);
    }

    public void AddAnimationClip(AnimationClip animationClipToAdd)
    {
    // Add the new Animation Clip to the Animation Track
    _animationTrack.CreateClip(animationClipToAdd);

    TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);
    }
     
  2. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    Answering my own question in case this helps someone else. Once you have the timelineClip it is eazzee piezee to get to the AnimationPlayabelAssets simply typecast the asset reference:

    For example
    // Set translation offsets
    AnimationPlayableAsset animationAsset = timelineClip.asset as AnimationPlayableAsset;
    animationAsset.position = new Vector3(offsetX, 0, offsetZ);
    animationAsset.eulerAngles = new Vector3(0, rotationY, 0);