- Home /
GetButton will only play animation when held down
How do I play an animation completely with Input.GetButton ? It'll only play the complete animation if the button is held down.
if (Input.GetButton ("Attack1")&& grounded==true && IsMoving==false) {
Anim.Play("Attack1");
IsAttacking=true;
}
That is the snippet of code I am using for attacking. So far it works, but only if I hold down the button. I want it to play completely tho. Any help ?
Why not use $$anonymous$$ecanim? It's much simpler
I am using mecanim. It's 2D sprite. Anim is a reference to an Animator component. I fixed it my own way. I use coroutine's to detect if an attack is going and it plays the animation.
But, you should use Anim.SetBool/Float/Int
I see you fixed this your own way, but for reference you should look at how you are handling input:
// This will play the animation while the button is being held down
if (Input.GetButton ("Attack1")&& grounded==true && Is$$anonymous$$oving==false) {
Anim.Play("Attack1");
IsAttacking=true;
}
// This will play the animation when the button is pressed
if (Input.GetButtonDown ("Attack1")&& grounded==true && Is$$anonymous$$oving==false) {
Anim.Play("Attack1");
IsAttacking=true;
}
// This will play the animtion when the button is released
if (Input.GetButtonUp ("Attack1")&& grounded==true && Is$$anonymous$$oving==false) {
Anim.Play("Attack1");
IsAttacking=true;
}
I tried GetButtonUp , and it did the same thing.
In that case, the problem may exist in the $$anonymous$$ecanim state machine. If a state has no exit time (i.e. looping animations) and the state can be interrupted, it will leave in the middle of the animation depending on the conditions. This is one of the reasons that most people use parameters to drive the states and transitions rather than calling Animator.Play() to jump directly to a state.
Answer by Shiro_Rin · Oct 18, 2015 at 03:30 AM
I fixed it using coroutine's. My own way I guess
Answer by DireDoesGames · Oct 18, 2015 at 09:13 AM
You might be able to put the Anim.Play("Attack1");
in the IsAttacking
variable.
If my answer helps you please accept it and up vote it! Thanks!
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
change order in layer via animation window? 1 Answer
2d Top Down Enemy Control (LoZ type) 0 Answers
How to have an animation loop only twice 1 Answer
2D Animator: how to get rid the animation "blending" 0 Answers