x


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");

 }

}

more ▼

asked Nov 07, 2011 at 08:26 PM

Dreave gravatar image

Dreave
137 255 180 182

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Nov 08, 2011 at 06:28 AM

The Arc Games gravatar image

The Arc Games
422 1 4 8

Sure. Just explain what you don't understand and we'll get it sorted.

Nov 13, 2011 at 09:34 AM The Arc Games
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9498
x7209
x7052
x23

asked: Nov 07, 2011 at 08:26 PM

Seen: 2080 times

Last Updated: Nov 13, 2011 at 09:34 AM