- Home /
How to play the animation completely on mouse click
Hi! We're having some troubles with the animation sistem. Basically, we want to make the attack animation play on mouse click, but it just does the first frame of the animation, while we want the animation to complete when you press the mouse button.
This is the code we are using: void Update () { if (Input.GetKeyDown (KeyCode.Mouse0)) animator.SetBool ("BaseAttack1", true); else animator.SetBool ("BaseAttack1", false); }
@Joppix Ins$$anonymous$$d of setting bool "BaseAttack1" to false use exit time in the animator controller. Just make "BaseAttack1" to true on mouse click
Default-------->BaseAttack1=true--->Animation Play----->Exit time---->Default
Hope you understand what i mean
Answer by aditya007 · Nov 23, 2016 at 12:33 PM
but it just does the first frame of the animation
Because that's what you're telling Unity to do. Update() runs every frame. So, basically what you're doing here is playing animation at mouse click and then stopping it the in the next frame.
Input.GetKeyDown (KeyCode.Mouse0))
will return true at Mouse click, so the bool will be set to true and animation will start. But, user stopped pressing mouse and in the next frame you are setting the bool to false, causing it to stop the Animation.
What you should do is remove the else
part and in the Animator Window, select the transition and check "Has Exit Time".
Also, mouse click could be used as a swich for animation play/stop. By setting the BaseAttack1 value to negative one.
void Update () { if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.$$anonymous$$ouse0)) animator.SetBool ("BaseAttack1", !animator.GetBool("BaseAttack1")); }
Well, you helped me, but i had to keep the else part. That's because if the animation would be repeated endlessly, i just cheked " Has Exit Time ". I'm gonna post other question soon, 'cause we are still learning and we have several problems to solve, so stay tuned xD.