- Home /
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");
}
}
Answer by The-Arc-Games · Nov 10, 2011 at 06:26 AM
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.
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 L$$anonymous$$B it only plays the animation once like before. Could you please help?
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.
Thanks i used this but for walking and used "vertical"
Answer by Jehos · Nov 09, 2011 at 05:56 PM
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.
This script dosent seem to do anything, I have checked the console for errors but theres nothing to blame in there, any ideas?
It was meant as a guide rather than as a code.
The idea is that you change the Wrap$$anonymous$$ode of your AnimationState depending on the user pressing or releasing the L$$anonymous$$B:
If the user is pressing the L$$anonymous$$B, animation should be set to Loop. If the user is not pressing the L$$anonymous$$B, 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.
Your answer
Follow this Question
Related Questions
how to run Sprint 1 Answer
When my button is pressed the action reapeats 1 Answer
Third Person Help 4 Answers
Animation Problems 1 Answer
Character controller problem? 2 Answers