- Home /
Animation blending.
I've tried working with a couple of scripts for my running animations, but they don't seem to be working. What I basically want to do is animate a character's walk and run animations seamlessly. When the character's speed is less than 1, it's just walking. Between 1 and 25, it slowly graduates toward fully running, and at 25 and over, the character is full-on running.
How is this done? Perhaps there is a good reference aside from the Character Animation tutorial in Unity's site? (That one didn't help me learn, nor did it come close to working.)
Answer by StephanK · Jun 17, 2010 at 08:52 AM
Normally walk and run animations don't get blended in that way. The normal case would be to check if the speed is over a certain threshold and then crossfade to the run animation. Depending on how long the fade is this will be seamless. To do this use:
animation.CrossFade(newAnimClip, time);
If you want to do it your way you would have to set up the two animations with weights and then change both weights according to your speed. I never tried this and it will probably look odd, but to do this you would have to do something like this:
float maxSpeed = 25; float weightRun = speed / maxSpeed; float weightWalk = 1 - weightRun;
animation.Blend("walk", weightWalk, 0.1); animation.Blend("run", weightRun, 0.1);
As I said this isn't tested, but should give you an idea.
Ah, this helped a bunch. It seems my problems earlier were due to a bad calculation and forgetting to account for an idle animation.
I Clamp01'd the weightRun just in case, and it seems to work great. It does look a bit odd at the moment due to the huge gap between speeds, but I should be able to fix that.
The speed is 'cause it's a Sonic fangame. xD
Thanks for your help, and sorry if my question was vague.
I'm new to unity so I'm not sure if this is a new feature or not, but I'm using a blend tree in my project and that seems to do what the OP was talking about. What this answer mentions seem to be what are called transitions. I believe these are both supported in the mecanim animation system. Just FYI for future readers.
Answer by runevision · Jun 17, 2010 at 09:09 AM
You say that it doesn't work, but that doesn't provide much information. How does it not work? What results or errors do you get?
If your walk and run animations have different lengths (which is likely) you can use Animation.SyncLayer() to keep them synchronized.
Your answer
Follow this Question
Related Questions
Play 2 animations at once 4 Answers
What am I not getting about animation weight blending? 4 Answers
Animation events play for all animations when blending? 1 Answer
Confusion about animation blending, weights 4 Answers
Blend Shapes in Unity? 4 Answers