Hi there, hoping Chris or someone else is able to please explain to me like I'm a 5-year old, about how to use Timeline to do something simple. I've tried many times unsuccessfully to figure this out. Have tried Google Gemini but it's no luck either.
I have my 2D player character as a prefab - it's spawned in at runtime and works well with ActionLists, etc.
I've got a LOT of custom single-use animations cluttering up the player animation controller, and would like a better / more efficient way of playing one-shot character animations. I'm using 'Sprites: Unity' so the nodes are standalone and don't have a spider-web of interconnected nodes.
Timeline seems like a good choice as I believe an animation doesn't need to be in the animation controller for it to play in timeline, but I just can't seem to get any ways with it.
How would I use timeline to:
a) get my player character to walk across the screen (I haven't been able to do this), then
b) say 'hi' once he's reached his destination, then
c) play a custom animation
Another question - because I'm using 'Sprites: Unity' is there even a limit of adding one-shot animations to the animation controller? I'm guessing though that all these animations are loaded into memory at the start, and if I'm only going to use the animation once, no use keeping it available for use at all times like how a character's walk or talk animation is needed.
Sometimes I teleport the player off-screen, create a new empty Game Object in a scene, put an 'Animator' component on it and make it play the custom player character animation, then teleport the player back to the location where the custom animation ends - which is workable, but not sure if it's the best way to do things.
Thank you.
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
There's no hard-limit to how many Animations a Controller can have, but certainly they'll start to have an impact on memory usage if there's many. Using Timeline for one-off animations is a good idea for cases like this.
From your described use-case, you'll want to have 4 tracks:
A few things to bear in mind:
Thanks for this Chris, this is great info to take another crack at it. So at a zoomed out view, I'm guessing I should have a temporary copy of my character prefab in the scene to work with while building the timeline (perhaps by dragging the character into the timeline to get the option of which tracks to create?), and later replace this it the spawned-in-at-runtime character by using the 'Remap binding' options? In 2D, is a person able to play back the timeline / scrub along the timeline in editing mode (like I think you were able to do with the 3D example if I remember correctly), or like a ActionList can a person only see how the timeline plays out after hitting 'play'?
Thanks also for the 3D Primer example - have tried watching that a few times in the past, somehow I couldn't quite get it to work in 2D for some reason (I forget why) but am sure the 3D example should translate well to 2D too, think I just need more time tinkering with it, thanks.
Yes, when it comes to affecting spawned characters you'll need to work with a copy of that character in the scene, and then remap each track's bindings to the "live" character at runtime.
How a Timeline reacts to being scrubbed is down to the individual track. Animation tracks should scrub without issue, but the Character Animation Track only works in Play mode.
Hi Chris, thanks for this info. Finally got somewhere with things.
1. Struggled to do or add anything to the timeline, but this was because the Director needs to be selected, not the timeline itself. I pressed the little 'lock' button to keep the director active.
2. I was struggling to get the remapped bindings to work on my spawned character prefab player, only realising because my player prefab didn't have an 'Animator' component on its root. When I dragged the prefab into the empty slot in the timeline, it created an animator on the scene's prefab root, but not on my original prefab. Adding an animator component to my OG prefab allowed the rebinding to work when hitting play.
3. The custom animation after walking wasn't working, the first frame of the custom animation was being held during translation / when moving across the screen, where the walking animation should have automatically been playing. I realised this was because I needed to turn off 'Pre-Extrapolate' on the custom animation's clip in its inspector to turn off the little infinity sign to the left of it. Now, the walking animation automatically played (because I had a 'character 2D animation shot' on Clip 2 - AC Character Animation 2D Track, spanning the duration of the movement) correctly and the custom animation played only when the playhead hit it.
The problem I'm facing now is trying to get the rebinding working for the 4th track ("4. Another Animation track, to play the custom animation"). My animations are played on the sprite-child. "If so, your 4th of the above tracks will need to be bound to this same child Animator - not the one on the root (which itself is necessary for the 1st track)." I can get this to work with the dummy prefab in the scene, I've dragged its sprite-child into the empty slot on the track and the custom animation plays correctly when I hit play. But I'm struggling to get the rebinding to my spawned player prefab to work, it won't play the custom animation and the player just stands still. In the Action, I have the track 'Bind to Player?' selected, and wonder
I thought maybe it was because the sprite-child of the dummy scene player prefab had a 'char' script on it. When I added the char script to the sprite-child of my original prefab, I get a lot of different flickering animation frames. I also thought maybe my sprite-child of the original prefab needed a 'Constant ID' - I added it, and redid the bindings, but that didn't work either.
Wonder please if you have any tips I could try to get that re-binding working correctly to my spawned player prefab for the custom animation track? (The speech rebinding is also working correctly btw, but am trying to tackle things one by one).
Don't add a "Char" component to your character - and certainly the sprite child shouldn't have anything.
If your "regular" Animator is on the sprite-child, then you can't use Bind to Player? here - because this will tie it to the root Animator. Instead, open up the Player prefab in Unity's Prefab Mode, and then drag the sprite-child into the Bind to field. This should link the object with the field via Constant ID number.
Thanks Chris. Yes, my regular ‘animator’ component is on the child sprite. I was able to open the original/main character prefab and drag and drop the sprite-child into the bind slot for the Action, but as soon as I exit the prefab, the connection is lost, and it reverted to the character prefab in the scene. If I deleted the dummy character prefab in the scene before doing this, then the slot would show empty/nothing when I exited prefab mode.
The sprite-child has a Constant ID component on it, and it remembered the ID number while I had the original character Prefab open. So, while the movement and walking, and dialogue was working for my binded original spawned character prefab, all the custom animations was playing on the dummy scene prefab.
What DID work however, was dragging the entire Prefab (not just the sprite child) into the custom animation track binding Action, and only by unchecking/disabling or deleting the dummy scene prefab, would the spawned character Prefab correctly play the custom animations.
I'm not sure why this is, but it does work consistently.
As soon as I re-enable the dummy Prefab, custom animations play on it.
Planning to make a simple video about this before I forget this useful information, thank you for the help! 😅
[Sorry - in the post above, the second image should be directly after the first image demonstrating the lost connection after exiting the prefab view, and both images should be a paragraph higher. Unnecessarily confusing, only noticed after I couldn't edit post].
I do have one or two more related questions please - considering an alternative for efficiency; teleporting the player offscreen and running an animator and its animation controller on an empty game object that plays just that custom character animation.
In AC, do all player characters AND all NPCs have all the animations that are in their animation controllers, pre-loaded into memory, even if an NPC appears much later in the game, in Scene 50?
And does AC preload all animations in ALL animation controllers (eg in game objects in the scene) all at once across the game, or does it load in only ones that are in the current scene and then offload them on scene exit?
Considering this as an alternative to using Timeline, if it's also memory efficient, but would be worried that it's the same result if it's just moving an animation from one controller to another if they're all going to be loaded into memory in any event (in that case, may as well just keep all the player's custom character animations in its controller).
I asked Gemini and it suggests that AC NPCs and scene-objects with animators load and unload per scene and aren't too much of a concern, but it's the Player's animation controller that's a persistent game object and all the animations in controller are preloaded into ram for the duration of the game, and that can get bloated. So, I think I may want to move a lot of the single-use animations out of the player's character controller and replace with timeline or either 'teleport and play custom on an empty game object' if that's a suitable alternative.
Gemini suggested the 'teleport player and dummy replacement object' is not a bad alternative, but that timeline is superior. Just need to get used to it I think.
That's correct - if an object is only present in a given scene, then its Animator is only in memory for the duration of that scene.
I'm not sure I 100% follow the prefab child issue, though. If you can share a video that demonstrates it, that'd help clear things up thanks.
Thanks Chris, this is useful info. And guessing the same thing with NPCs; their animation frames from all the animations present in their animation controllers aren't all loaded into memory, like how they are for the player characters? NPCs are treated as scene objects, only loaded in and out during the scene?
The prefab child issue disappeared when I reset back to an earlier branch and did a video explainer, then I could use the method you suggested with the sprite child and it worked as intended.
A video in case useful for anyone: