- Home /
Problem with Hash IDs & End of animation detection
Hello everyone,
I would like to detect the end of attack animation.
First of all, I tried to use Animation Events but they don't work (sometimes when animation was very fast the event was not triggered).
I decided to create a custom method for detect an animation changed :
private void CheckEndOfAnimation()
{
var state = CharAnimator.GetCurrentAnimatorStateInfo(0);
if (Attacking)
{
if (!_checkEndAnim)
{
if (_currentAttackState != state.nameHash)
{
_currentAttackState = state.nameHash;
_checkEndAnim = true;
}
}
else
{
if (_currentAttackState == state.nameHash)
{
if (state.normalizedTime >= 0.99f && !CharAnimator.IsInTransition(0))
{
if (_currentAttackTrail != null)
{
_currentAttackTrail.transform.parent = null;
}
Attacking = false;
_checkEndAnim = false;
}
}
else
{
if (_currentAttackTrail != null)
{
_currentAttackTrail.transform.parent = null;
}
Attacking = false;
_checkEndAnim = false;
}
}
}
else
{
_currentAttackState = state.nameHash;
}
}
It seems to work for most cases... but sometimes "state" variable (CharAnimator.GetCurrentAnimatorStateInfo(0)) change in the middle of an animation (without reason).
For debug this problem, I would like to know what is the new animation... but i can't get the state name.
I tried to instantiate (in Start method) a dictionnary with state name and hash ids for i can't get the hash (wich is in substate system).
// Works
AnimHashes.Add(Animator.StringToHash("Base.Idle"), "Idle");
// Doesn't work
AnimHashes.Add(Animator.StringToHash("Base.RightWeakAttack2"), "RightWeakAttack2");
AnimHashes.Add(Animator.StringToHash("Base.Attacks.WeakAttack.RightWeakAttack2"), "RightWeakAttack2_TEST");
Thanks for your help,
Best regards.
Answer by RemiR · Nov 09, 2014 at 06:41 PM
After spent the whole day to debug I found a solution :
Firstly, I found how get an hash id of animation clip (inside substate of substate animator).
Base => Attacks (substate) => WeakAttack (substate) => RightWeakAttack1 (selected animation)
I think the documentation is not complete enough and this page should be updated :
http://docs.unity3d.com/ScriptReference/Animator.StringToHash.html
You must use :
Animator.StringToHash("WeakAttack.RightWeakAttack2")
Secondly, I forgot a condition to check the end of animation : Verify that the trigger animation boolean equals false.
I give the solution here for those who have the same problem
Best regards,
Old question, but people like me still search their way here. $$anonymous$$aybe fullPathHash didn't exist when this was asked:
If you use AnimatorStateInfo.fullPathHash ins$$anonymous$$d of AnimatorStateInfo.nameHash you don't have this problem. That's the hash of the complete path including the layer name and all parent state engines, eg. "Base.Attacks.WeakAttack.RightWeakAttack1". This way the hashes aren't ambiguous when you use the same state name in different substates, and you can use the full hash with Animator.Play as well.
Answer by · Mar 04, 2016 at 03:03 PM
Your answer
Follow this Question
Related Questions
Bug animation transitions, or am I the bug? 0 Answers
2D Animation does not start 1 Answer
Animator problem in Create with Code. in Unit 3 1 Answer
Mecanim CrossFade transition interruption issue 3 Answers
Unity not playing Legacy animation 0 Answers