Search Unity

Question Playing gun animation only once on left-mouse click

Discussion in 'Animation' started by KeyboardStudios, Jul 14, 2020.

  1. KeyboardStudios

    KeyboardStudios

    Joined:
    Jul 11, 2020
    Posts:
    9
    Hello guys!

    The thing is that I want to play a firing animation just once when we press the left mouse button. And I have set it up so that if you keep pressing the left mouse button forever, the player will shoot just once and then stop shooting. You have to manually press the button every time you want to shoot. The problem is that the animation of shooting still plays if you keep holding the mouse button down though the firing and the damaging stops. Any suggestions on how can I make it so that whenever someone keeps holding the button the animation too just plays once and stops. I have tried setting up a timer to track the time the button is pressed and if it goes out of a certain range, set the parameter "isFiring" to false using Time.time. But either I totally flunked at coding it or visual studio just envies me. So, I would gladly accept any help.

    Thanks in advance!!
     
  2. TestUser001

    TestUser001

    Joined:
    Jun 22, 2018
    Posts:
    5
    Code (CSharp):
    1.  
    2.         if(Input.GetMouseButton(0))
    3.         {
    4.             isFiring = true;
    5.         }
    6.         else
    7.         {
    8.             isFiring = false
    9.         }
    10.  
    Is this what you mean?
     
  3. KeyboardStudios

    KeyboardStudios

    Joined:
    Jul 11, 2020
    Posts:
    9
    Well, thanks for the suggestion but I want to play the firing animation just once whether the player holds on to the button or anything. But this code will repeat the animation again and again the whole time the button is pressed. Somewhat like this -
    if(button is pressed){
    just play animation once and then stop
    }

    Thanks again for the suggestion tho