x


Loop animation on button hold

how would I change this script so that if the LMB is held down it loops the animation until it has stopped being held but if pressed once it plays the animation once?

function Update () {

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

animation.Play("shoot");

}

}

more ▼

asked Nov 09 '11 at 05:44 PM

Dreave gravatar image

Dreave
122 82 94 96

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

2 answers: sort voted first

Alternatively, there's a simpler approach.

function Update () {

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

myanimationisplaying = true;  //boolean value. declare it to false

}

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

myanimationisplaying = false;

}

if (myanimationsiplaying == true)
  animation.Play("shoot");
}
//the rest of the code

Which is how you usually logically implement this kind of functionality.

more ▼

answered Nov 10 '11 at 06:26 AM

The Arc Games gravatar image

The Arc Games
377 1 4

im having problems getting it to work, I have no errors in the console
I have my script attached to my gun but when I press the LMB it only plays the animation once like before. Could you please help?

Nov 10 '11 at 04:12 PM Dreave

this comment '//boolean value. declare it to false' actually meant that you need to declare the variable to be able to use it! 'var myanimationisplaying : bool;' and then set it to 'false' on start.

Nov 13 '11 at 09:32 AM The Arc Games
(comments are locked)
10|3000 characters needed characters left

You could change the WrapMode depending on the LBM being pressed.

Something in the vicinity of:

function Update()
{
  if (Input.GetButtonDown("Fire1"))
  {
    if (!animation.isPlaying)
    {
      animation.Play("shoot");
    }
  }
  animation["shoot"].wrapMode = Input.GetButton("Fire1") ? WrapMode.Loop : WrapMode.Once;
}

This will cause the animation to loop if the button is being held, and to stop looping when the button is not being held. Keep in mind when you release the mouse button, animation will not immediately stop, it will rather reach to the end and then stop.

more ▼

answered Nov 09 '11 at 05:56 PM

Jehos gravatar image

Jehos
91 1 1 2

This script dosent seem to do anything, I have checked the console for errors but theres nothing to blame in there, any ideas?

Nov 09 '11 at 06:12 PM Dreave

It was meant as a guide rather than as a code.

The idea is that you change the WrapMode of your AnimationState depending on the user pressing or releasing the LMB:

If the user is pressing the LMB, animation should be set to Loop. If the user is not pressing the LMB, animation should be set to Once.

Animation start doesn't require anything special in coding, except to make sure you don't re-execute the Play() command while the animation is already playing.

Nov 09 '11 at 07:52 PM Jehos
(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:

x5068
x3772
x3727
x786
x32

asked: Nov 09 '11 at 05:44 PM

Seen: 2237 times

Last Updated: Nov 13 '11 at 09:32 AM