2
\$\begingroup\$

I have trouble with Animator in Unity2d. I have a main character for my game. The character has his own animation clip for fighting. When the player touches the screen, i am starting animation. When the player touches the screen slowly (every second) - everything is working fine, but when the player touches the screen fast, the animation clip is playing about 10 times, and then nothing happens (the clip isn't playing). I've checked - the touch event is triggered normaly, may the problem is with animator. Does anyone have ideas how to fix this issue?

public bool UseAxisInput = false;
private Animator Anim;

// Use this for initialization
void Start () {
    Anim = GetComponent<Animator>();
}

// Update is called once per frame
void Update () {
        for (var i = 0; i < Input.touchCount; ++i) 
        {        
            if (Input.GetTouch(i).phase == TouchPhase.Began)     
            {
                Anim.SetBool("IsFighting", true);
            }
        }
}

May be this is because of that at the end of my animation i have an animation event which calls the method, that stops animation. This is animation clip

This is my method, that is called, after the animation ends

public void stopFighting()
{
    Anim.SetBool("IsFighting", false);
}
\$\endgroup\$
  • \$\begingroup\$ Why not use a Trigger instead of a Boolean parameter and scrap the AnimationEvent completely? \$\endgroup\$ – McAden Oct 29 '14 at 22:30
  • \$\begingroup\$ Can you please tell me more about this? I know very little about triggers((( \$\endgroup\$ – bukka.wh Oct 30 '14 at 8:06
  • \$\begingroup\$ A Trigger (in regards to animation in Unity) is essentially a Bool that automatically turns itself off after the state change. \$\endgroup\$ – McAden Oct 30 '14 at 17:30
  • \$\begingroup\$ Thank you, McAden! I have edited my code according to triggers and now everything works good. \$\endgroup\$ – bukka.wh Oct 31 '14 at 8:32
  • \$\begingroup\$ Glad that worked for you. I posted it below as an answer. \$\endgroup\$ – McAden Oct 31 '14 at 16:58
2
\$\begingroup\$

Use a trigger within the Animator instead of a boolean. Remove the AnimationEvent from the animation and the stopFighting function you created. Then, once you've changed the parameter type in the Animator you can change the code to:

Animator.SetTrigger("IsFighting");

Then once it triggers it will turn itself off after the animation state evaluates it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.