Search Unity

Play punch animation sequentially if mouse is pressed

Discussion in 'Animation' started by n1ght_A, Jan 3, 2019.

  1. n1ght_A

    n1ght_A

    Joined:
    Oct 25, 2018
    Posts:
    3
    So if I hold down LMB I want first the right punch animation to be played and if the player is still holding, then play the left punch as well. Repeat.

    I have this code. The function is called inside an if(Input.GetMouseButton(0)) statement.

    When I hold down the mouse button it sometimes plays one of the animations twice (or more) in a row before the next animation plays

    Code (CSharp):
    1.  
    2.  
    3.      bool punchRight;
    4.  
    5.      public void punch()
    6.     {
    7.         if (punchRight)
    8.         {
    9.  
    10.             GetComponent<Animator>().SetBool("LeftPunchBool", false);
    11.             GetComponent<Animator>().SetBool("RightPunchBool", true);
    12.  
    13.  
    14.             punchRight = false;
    15.         }
    16.  
    17.         else if (!punchRight)
    18.         {
    19.             GetComponent<Animator>().SetBool("RightPunchBool", false);
    20.             GetComponent<Animator>().SetBool("LeftPunchBool", true);
    21.  
    22.  
    23.             punchRight = true;
    24.         }
    25.  
    26.  
    27.     }
     
    Last edited: Jan 3, 2019