Machine gun animation

I have a recoil animation on my machine gun which I have animated inside unity, I have wrote a script which plays the animation when the LMB is clicked, but what I want is that if the LMB is held down it fires the recoil animation 30 times and then stops. How would I do this?

function Update () {

if ( Input.GetButtonDown("Fire1") ) {

   transform.animation.Play("shoot");

}

}

Well this is a very logical problem that involves many aspects of implementation.
For a start, you need to create a timing cycle, that only fires the animation when the animation lenght has passed.

So you create a variable and assign it the

Time.time value

and then in the condition, you add

&& (Time.time - mylastshoot > animationlengthtime)

in the condition, to basically only shoot again if the animation length has passed since the last shoot.
And then of course you set your ‘mylastshoot’ variable to Time.time again.

Additionally, since you need to only shoot 30 times, you create a counter variable ‘shotsleft’ and assign 30 to it.

and modify the condition again,

&& (shotsleft > 0)

And then of course you decrease shotsleft after you play the shoot animation.