- Home /
Animation Blending Problem
I know this question has probably answered a million times, but I need a personalized answer if you don't mind :). What I'm trying to do is blend Walk right, walk left, walk forward, etc... All together seamlessly, and of course idle. Right now I'm dealing with CrossFades which doesn't seem to work very well. Im hoping you guys know of a better way to blend animations together. Here is my current code:
function Update () {
if(Input.GetKeyDown("w") || Input.GetKeyDown("s")){
animation.CrossFade("Walk Forward",0.1);
}
if(Input.GetKeyUp("w") || Input.GetKeyUp("s")){
animation.CrossFade("Idle",0.2);
}
if(Input.GetKeyDown("a")){
animation.CrossFade("Walk Left",0.1);
}
if(Input.GetKeyUp("a")){
animation.CrossFade("Idle",0.2);
}
if(Input.GetKeyDown("d")){
animation.CrossFade("Walk Right",0.1);
}
if(Input.GetKeyUp("d")){
animation.CrossFade("Idle",0.2);
}
}
You say it doesn't work "very well." So it kind of works but is glitchy? What's wrong with it?
The usual CrossFade time is 0.3 seconds (if you don't even use a second number) -- 0.1 might be a little "snappy."
No matter what value i put in, it doesn't fade at all. I was hoping for another method that doesn't include crossfading to exist.
You can replace CrossFade with Play, but CrossFade is just better.
So, what happens? "it doesn't fade at all."? So do they play when you press the keys, but jerky? Or does it never play any animation at all? Or does it jerk a little when you press a key, but then quickly go back to idle?
Answer by Griffo · Aug 03, 2012 at 12:49 PM
Try something like this
animation["Walk"].layer = 1; // Place on higher level so it will take priority over other animations
animation["Walk"].wrapMode = WrapMode.Loop;
animation.CrossFade("Walk");
That will make it so playing walk once will "disable" all other animations, with walk playing forever.
A nice feature of CrossFade is it auto-fades any other animation on the same layer. That allows CrossFade(idle/left/right) to also fade out walk.
$$anonymous$$oving walk up to layer 1 (they all start on zero, for the OP,) gets rid of that. You'd need to stop (or blend to weight 0) walk by hand.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075122im_/https://answers.unity.com/themes/thub/images/avi.jpg)