Thursday, April 16, 2015

unity - How to avoid spaghetti in the animator?


I am working with animations in Unity and so far I have made only four (Up Down Left and Right) and my animation window looks following... enter image description here


Problem is that if I was moving Up I want hero to stay looking Up not go to 'neutral idle' (or however he started), but that means I have 4 Idle positions... and each of them can transition to Up, Down, Left or Right and they go exponentially with each additional action...


Which makes me feel like I am doing something wrong....


Is there a better way of making transitions between different actions?



Answer



You can solve this type of problem with BlendTrees (in general its wise idea to often rely on blend trees). Blend tree is kind of a multi-animation state that based on a (float) parameter(s) decides which of the animations it contains to be currently active (or alternatively the resulting animation can be a blend of several animations). Even when the animations are not blended, but swapped, the swap happens mid-animation (i.e if the "exiting" animation is at 35% the new one is played from 35% forward). From the "outside" all this is hidden as if it's one simple/single state.



For example you have a blend tree with 8 sprite animations (idle with 4 main and 4 diagonal directions). There are 2 parameters in this blend tree - "horizontal" and "vertical". And based on those 2 parameters the blend tree will decide what animation to play. Based on "horizontal" parameter the blend tree will choose between left/right animation and "vertical" parameter will decide the top/down animation. Then when combined you will have all 8 animations with diagonals (it's the same idea that's behind joypads/joysticks).


enter image description here


It's a bit confusing so, even if StackExchange doesn't like links, I will link short example video I made. Notice the top-right part of the screen. The gray dots are the "states"/animations and the active one is the one closes to the red dot. As said in the beginning, it can be even more complex and each animation to have an influence/strength based on its distance to the red dot, resulting in a blend between several animations.


From the outside its just one state(blend tree) and when you apply the same concept for walk and attack you will have only 3 states linked.


Blend Trees are created with right click on an empty Animator area Create State -> From New Blend Tree. From there it's all in the Inspector. With the blend tree selected you can pick the type of the blend tree (1D, 2D, etc.), add animations, position them (relative to parameter values) and select the controlling parameters.


If you want more info on Blend Trees next are videos from Unity - Unite 2016 and Unity Tutorials.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...