- Home /
How to animate an instantiated object in the timeline/director
Howdy!
I'm having a problem using the Timeline to animate an instantiated object and I can't find the answer anywhere. The issues is this:
I want to animate my player/npc prefab instantiated during runtime. But the timeline seems to only work with objects, or animators that are in the scene. So what is the best solution for this?
I tried setting the animator via director binding but it asks me for the track name and object, which I was not able to access or set and the information on that is kinda hard to understand.
So I am stuck with this issue. I would really appreciate any help on this issue!!
Thank you!
Answer by seant_unity · Aug 05, 2020 at 11:45 AM
PlayableDirector.SetGenericBinding(AnimationTrack, Animator) is the correct method to set a track binding for an animation . To find a reference to the track, you can use timelineAsset.GetOutputTracks() and search the tracks, and use the one with the correct name.
var timelineAsset = playableDirector.playableDirector as TimelineAsset;
var track = timelineAsset.GetOutputTracks().FirstOrDefault(t=>t.name == "MyTrackName");
playableDirector.SetGenericBinding(track, myAnimator);
The name can be set in the inspector when a track is selected in the Timeline Editor.
Answer by codywulfy · Aug 05, 2020 at 09:38 PM
Hi @seant_unity
Thank you so much for replying, I'm trying to use the code but I'm not very good at coding.
What does this do?... "FirstOrDefault(t=>t.name == "MyTrackName");"
because it's not working. I can get as far as timelineAsset.GetOutputTracks() but I don't get the rest. Can you help me understand this? thank you!
It's using System.Linq to do the following - effectively find the first track in the timeline with the given name.
TrackAsset track = null;
foreach (var t in timelineAsset.GetOutputTracks())
{
if (t.name == "$$anonymous$$yTrackName")
{
track = t;
break;
}
}
Oh I see! Thank you so much @seant_unity for your help! it's still kinda confusing for me but that should do the trick :) thank you