- Home /
Unity 3.5 Animation : stop one layer
Hello everyone. This question must have been answered already, but I can't find it anywhere. Also, I'm fairly new to animation. In this example by Unity, we are considering that the player will shoot only once
animation["shoot"].wrapMode = WrapMode.Once;
Let's consider that I want "shoot" to be a looping animation. As long as the player keeps the button "shoot" pressed, the character shoots.
so, on layer 0 I have "idle" or "walk" looping, and on layer 1 I have "shoot" looping.
My question is simple : When the players stops shooting, how do I stop "shoot" so the arms to go back to their "walk" or "idle" animation ?
(Edit : Of course I mean by not using Stop("shoot"), which wouldn't be smooth)
Bonus question : Is there a way to "Crossfadeout" to do so ? (the arms go smoothly back to their "walk" or "idle" animation).
Thanks A lot for your answers
Answer by Setsuki · Nov 28, 2012 at 10:42 AM
Well, I finally found a workaround by playing with the animation weight:
CrossfadeOut(Animation animation,string animationName,float timeCrossfade)
{
float originalWeight = animation[animationName].weight;
while(animation[animationName].weight>0)
{
animation[animationName].weight-=(Time.deltaTime/timeCrossfade)*originalWeight;
yield return null;
}
animation.Stop(animationName);
animation[animationName].weight=originalWeight;
}
Or, if you're allergic to divisions like me :
CrossfadeOut(Animation animation,string animationName,float timeCrossfade)
{
float originalWeight = animation[animationName].weight;
float decreaseValue = (Time.deltaTime/timeCrossfade)*originalWeight;
while(animation[animationName].weight>0)
{
animation[animationName].weight-=decreaseValue;
yield return null;
}
animation.Stop(animationName);
animation[animationName].weight=originalWeight;
}