- Home /
AnimatorStateInfo.IsTag not working?
Hi there, in my code I want to check the animation that the player is playing, when in a specific distance of a character. Here is the code:
void Update ()
{
if (!m_animator)
return;
// get info about this character anim state
AnimatorStateInfo thisCharStateInfo = m_animator.GetCurrentAnimatorStateInfo (0);
m_currentDistance = Vector2.Distance (transform.position, m_player.position);
// If we are running, keep increasing speed
if (thisCharStateInfo.IsName ("Base Layer.RunningBlend")) {
// Increase character speed automatically
m_speed += m_acceleration * Time.deltaTime;
m_animator.SetFloat ("Speed", m_speed);
if (Mathf.Abs (m_currentDistance) < m_attackDistance) {
m_animator.SetTrigger ("Attack");
}
} else {
// This character isn't running. Lets see what the player is doing
if (m_playerAnim) {
// Get info on PLAYER anim state
AnimatorStateInfo playerStateInfo = m_playerAnim.GetCurrentAnimatorStateInfo (0);
if (playerStateInfo.IsTag ("ATTACK")) {
Debug.Break();
m_animator.SetTrigger ("Death");
Utilities.PauseGame (10, 0f, this);
}
}
}
}
So, everything works as expected, except the line:
if (playerStateInfo.IsTag ("ATTACK")) {
I'm assuming that if I have animation states tagged 'ATTACK' in the player animation controller, that this would be true - but this condition never passes. When the above code is running I've been looking at the animator window and these states are definately entered into. So what is going wrong? Thanks for any help!
m_playerAnim is not a bool is the Animator (again) since in the next line m_playerAnim.GetCurrentAnimatorStateInfo (0); if you change m_playerAnim to m_animator it works fine, btw.
To clarify:this script is on an enemy.
m_playerAnim is a reference to the players Animator.
m_animator is a reference to this characters Animator
So what I'm trying to do is test if the player is playing any anims that are tagged with ATTAC$$anonymous$$ (so could be a $$anonymous$$ick or Punch)
Thanks
I know this is the question and not the answer, but I searched everywhere for how to check which animation in my substate machine was playing so I could trigger another animation, and seeing this provided me with the exact solution.
I simply added a tag to each animation, and then stored AnimatorStateInfo myState = anim.GetCurrentAnimatorStateInfo(0);
with myState.IsTag("$$anonymous$$yTag")
in the if boolean.
Answer by AntLewis · Jul 29, 2015 at 09:41 PM
Ok very simple error here. There were two objects tagged as Player in the gameobject and this code was finding the wrong one which had a different controller, in which the states werent tagged!
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Jumping animation Problem 2 Answers
Animator: Animate Physics does nothing? 0 Answers
Animator - Wait until animation finishes 3 Answers
"StateMachineBehaviour" Not working for sub sates ? 0 Answers