Blending Animation Layers
Hi, I have created an animation with 2 layers (as well as the base layer that combines the 2)
- A character mouth closing animation
- An idle animation
What i'm trying to do is have the mouth animation start when mouth opens, and then when this has finished, the idle animation kicks in (and then repeats). Can't seem to figure this out. I haven't played around much with the animation blending but i can't quite figure it out.
Animation layers in general seem a bit confusing, how do you specify to play 1 layer, followed by another, and then repeat it?
Hi Tom,
You can use the AnimationMixer component's startWithCallback function to trigger 2 different animations in sequence.
Here's an example of a script you can add to your project that will play a single "action" animation followed by a looping "idle" animation. Give it a shot and let me know if you have any specific questions about this approach.
//@input Component.AnimationMixer animationMixer
//@input string actionAnimName
//@input string idleAnimName
//@input int actionLoopCount = 1
if(script.animationMixer){
if(script.actionAnimName){
script.animationMixer.setWeight(script.actionAnimName, 1);
script.animationMixer.setWeight(script.idleAnimName, 0);
script.animationMixer.startWithCallback(script.actionAnimName, 0, script.actionLoopCount, onActionAnimComplete);
}
}
function onActionAnimComplete(){
if(script.animationMixer){
if(script.idleAnimName){
script.animationMixer.setWeight(script.actionAnimName, 0);
script.animationMixer.setWeight(script.idleAnimName, 1);
script.animationMixer.start(script.idleAnimName, 0, -1);
}
}
}
Let me know if you have any specific questions about how to set this up.
Best,
Peter
Hello,
Thanks Peter for this example! Do you (or anyone) know if it's possible to use startWithCallback and pass it a callback function that takes arguments? I notice that you pass just the function name onActionAnimComplete, without parentheses, but I wonder if there's a way, using anonymous functions, etc. to essentially call onActionAnimComplete and pass it the mixer and layer names each time.
My use case is that I want to apply this to multiple objects at once, and chain multiple anim layers for each, without stuffing my script full of specific methods for each individual callback that's supposed to happen.
Honestly I think the best solution is to stop trying to have these all take place in one master script file and just give each animated object a script component so it can handle its own animations. But I'm still curious if what I was trying would have been possible!
Hey Naomi!
Here's a version of the script that uses a wrapper function as the callback, which allows you to supply additional parameters to the anim callback:
Let me know if this works for you!
Best,
Peter
Ahhh, a wrapper function that just calls the main function! I see now how I was getting tripped up before and your solution appears to provide the missing piece to the workarounds I was trying.
However, I have since refactored the code so each animated object is called upon to handle its own animations. I must say it's much cleaner than what I had before, for a number of reasons!
Thank you so much for helping! Ack I simply love learning new things <3
Cheers,
Naomi