9
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ I've been trying to do this for a long time. Sadly, my conclusion is, you just can't. Animator ALWAYS turns into a web of spaghetti and we have to live with it. Take a look at this video though, it helps youtube.com/… \$\endgroup\$
    – Nevermind
    Commented Jun 8, 2017 at 14:44

2 Answers 2

9
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

The problem here is you're trying to tie everything together instead of using a trigger to switch between idles. You should be picking the idle animation using an if-else or a switch-case type of conditional. I've made a diagram that looks much neater (assume one of them is the default case):

enter image description here

Additional info:

Take a look at Animation Parameters on how to set parameters for the animator to recognize, State Machines for combining animations together, State Machine Behaviors for how to check the variables you've set.

\$\endgroup\$
6
  • \$\begingroup\$ Hi John, but how do I actually do it? Why would I use the trigger to swich between idles, or switch between idles at all? how do I pick idle animaton using if else? Also in diagram why I would move to idleleft and then to left and not left and go into idleleft waiting for next action, or this should be the other way around? \$\endgroup\$ Commented Jun 8, 2017 at 6:18
  • \$\begingroup\$ @MatasVaitkevicius You can set and check variables at the end or start of animations. You can make an empty animation that does nothing but check the case and then redirect it to the next part or make an abstraction layer out of it. I've included a few links to the manuals for more info. \$\endgroup\$ Commented Jun 8, 2017 at 6:27
  • \$\begingroup\$ So how do I make if else with parameters that would switch the transitions? There is no example all the mechanisms you linked just do manual transition like I did in the question.... \$\endgroup\$ Commented Jun 8, 2017 at 6:31
  • \$\begingroup\$ @MatasVaitkevicius You should really search a bit first: gamedev.stackexchange.com/questions/103611/… \$\endgroup\$ Commented Jun 8, 2017 at 6:35
  • \$\begingroup\$ But this is how I have done it. trigger fires of on action and starts the animation then it goes into idle for that direction... Now what you are saying (if I understand you correctly) that I could after animation instead of going to idle for that direction go to switch mechanism that would choose appropriate idle for me... but I haven't seen such a mechanism anywhere, could you copy it into your answer please.... \$\endgroup\$ Commented Jun 8, 2017 at 6:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .