Search Unity

Mecanim combo system occasionally stops and registers phantom clicks

Discussion in 'Scripting' started by joe-il-rustico, Jan 13, 2019.

  1. joe-il-rustico

    joe-il-rustico

    Joined:
    Jan 10, 2013
    Posts:
    2
    Hello everyone, first post ever so feel free to direct me to the relevant sections or point out other flaws in my post.

    I'm trying to get a simple 2d project to work, specifically the combo system. I'm using C#.
    The combo system I'd like to have would be one that at the first click of the mouse plays the first attack animation, then checks if other clicks were made and if they are more than 2 plays the second attack animation (otherwise goes back to idle), it then does the same with the third attack animation.

    I set this up with mecanim and animation events and it mostly works if not for two things that happen quite often:
    1 - when clicking the mouse only two times all three attack animations will play (it doesn't happen every time);
    2 - when rapidly clicking the mouse a point will eventually be reached in which no more animations are played. This last error I get really baffles me, I cannot for the love of myself understand what could be causing it, no messages or warnings are displayed.

    Below you can find the code I'm using, my animator window and the transitions details. I'd really appreciate any help or insight as I'm pretty new to mecanim and animations in general.

    Code (CSharp):
    1. void Start()
    2.     {
    3.         clickNumber = 0;
    4.         canAttack = true;
    5.         myAnimator = GetComponent<Animator>();
    6.     }
    7.  
    8.     void FixedUpdate()
    9.     {
    10.         if (Input.GetMouseButtonDown(0))
    11.         {
    12.             ComboStart();
    13.         }
    14.     }
    15.  
    16.     void ComboStart()
    17.     {
    18.         if (canAttack == true)
    19.         {
    20.             clickNumber++;
    21.         }
    22.  
    23.         if(clickNumber == 1)
    24.         {
    25.             myAnimator.SetInteger("Attack", 1);
    26.         }
    27.  
    28.         GameObject.Find("Look").GetComponent<LookAtMouse>().canRotate = false;
    29.     }
    30.  
    31.     //this gets called near the end of the attack animations
    32.     void ComboCheck()
    33.     {
    34.         canAttack = false;
    35.        
    36.         if (myAnimator.GetCurrentAnimatorStateInfo(1).IsName("Glove_Attack1") && clickNumber == 1)//if the first anim is playing and we only clicked 1 time
    37.         {   //return to idle
    38.             myAnimator.SetInteger("Attack", 0);
    39.             canAttack = true;
    40.             clickNumber = 0;
    41.             GameObject.Find("Look").GetComponent<LookAtMouse>().canRotate = true;
    42.         }
    43.         else if (myAnimator.GetCurrentAnimatorStateInfo(1).IsName("Glove_Attack1") && clickNumber >= 2)//if the first anim is still playing and at least 2 clicks were made
    44.         {   //continue combo
    45.             myAnimator.SetInteger("Attack", 2);
    46.             canAttack = true;
    47.         }
    48.         else if (myAnimator.GetCurrentAnimatorStateInfo(1).IsName("Glove_Attack2") && clickNumber == 2)//if the second anim is still playing and only 2 clicks were made
    49.         {   //return to idle
    50.             myAnimator.SetInteger("Attack", 0);
    51.             canAttack = true;
    52.             clickNumber = 0;
    53.             GameObject.Find("Look").GetComponent<LookAtMouse>().canRotate = true;
    54.         }
    55.         else if (myAnimator.GetCurrentAnimatorStateInfo(1).IsName("Glove_Attack2") && clickNumber >= 3)//if the third anim is still playing and at least 3 clicks were made
    56.         {   //continue combo
    57.             myAnimator.SetInteger("Attack", 3);
    58.             canAttack = true;
    59.         }
    60.         else if (myAnimator.GetCurrentAnimatorStateInfo(1).IsName("Glove_Attack3"))//if the last anim is playing
    61.         {   //return to idle
    62.             myAnimator.SetInteger("Attack", 0);
    63.             canAttack = true;
    64.             clickNumber = 0;
    65.             GameObject.Find("Look").GetComponent<LookAtMouse>().canRotate = true;
    66.         }
    67.         else
    68.         {
    69.             myAnimator.SetInteger("Attack", 0);
    70.             canAttack = true;
    71.             clickNumber = 0;
    72.             GameObject.Find("Look").GetComponent<LookAtMouse>().canRotate = true;
    73.         }
    74.     }
    My animator window
    AnimatorWindow.png.jpg

    Transitions details from idle to attack1. All the other transitions are exactly like this one except for the condition that becomes "Equals 2" for Attack1 to Attack2, "Equals 3" for Attack2 to Attack3 and "Equals 0" for going back to idle from any attack.
    Idle to attack1.jpg

    The (blue) animation event calls ComboCheck() (code above), I set up an animation event that calls the method in all of the attack animations.
    animationEvent.jpg

    Thanks to anyone that offers any help, feel free to tell me any best practices I'm not following regarding mecanim or anything at all and if you need more details in order to understand the problem please tell me and I'll provide them.