- Home /
How to get the current motion on Blend tree
I have created an Animator in Unity for my character. For now it is very simple, as I am still learning the basics of animations and blend trees.
However, I have run into an issue when I converted my three walk states into a blend tree. Now I do not know how to differentiate between which motion is playing, to be able to perform some certain logic based on which animation is playing.
My current Animator looks like this:
I have no problem figuring out if the character is in the idle1 state or WalkTree state by doing the following:
animator.GetCurrentAnimatorStateInfo(0).nameHash == Animator.StringToHash("Base Layer.idle1");
animator.GetCurrentAnimatorStateInfo(0).nameHash == Animator.StringToHash("Base Layer.WalkTree");
But how do I figure out whether it is the walk1left, walk1 or walk1right motion that is playing? Or which one is the dominating one? (Might be a more precise question.)
As animator.GetCurrentAnimatorStateInfo(0).nameHash
returns the same value, no matter what motion is playing when inside the WalkTree, I cannot use the same methods as shown above anymore.
Any help is much appreciated, thanks!
Answer by Mmmpies · Dec 14, 2014 at 12:36 PM
Blends do just that, blend or combine the animations so all you can get is the value of direction and base whatever you're doing on that.
Should add:
float curDirection = anim.GetFloat("Direction");
Al right, thanks man, for the quick responds. I think I need to re-evaluate my grand animator design and logic, as I now understand Blend trees a little bit better.
You're welcome. And thanks for the comment.
Really your script should be driving what the Animator is doing but it's still acceptable to get a value from the animator.
e.g. if you press to go left then SetFloat with the Direction to increase/decrease but lets say a swim$$anonymous$$g script has detected you've run into water and just gone below the level where you have to start swim$$anonymous$$g.
That swim$$anonymous$$g script sets a bool in the Animator isSwim$$anonymous$$g = true.
Your motion script checks if the Animtion bool isSwim$$anonymous$$g is true, it is. The animation speed and direction should be high so it applies the right swim$$anonymous$$g animation based on those but as swim$$anonymous$$g is different we need to get the speed float to know what force to apply to move the player.
Well that got a bit out of hand! Basically most of the time your script will be going hey Animatior do this, but you will need to go ask it what its current values are at some points.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174020im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D: initial sprite idle animation playing too fast upon game initiation until I input movement keys? 1 Answer
Dynamic thresholds runtime C# 1 Answer
Sebastian Lague Character Creation Tutorial animations not being triggered 0 Answers
Blend Tree Animation Problem 1 Answer
Blending animations for a bow with differing charges 1 Answer