- Home /
PLaying an animation on click
Im trying to get it so when you press Mouse0 (primary mouse button) it will play an animation called "punch"
Im kind of a "noob" at java but here is what I got:
@script RequireComponent(CharacterController)
public var punchAnimation : AnimationClip;
if(!punchAnimation) {
_animation = null;
Debug.Log("No animations, turning them off");
}
if(_animation) {
if(Input.GetKey (KeyCode.Mouse0)) {
animation.Play("punchAnimation");
}
}
Please help!!
Answer by crazyKnight · Apr 19, 2011 at 04:53 AM
code
if(Input.GetMouseButton(0)) { animation.Play("punch"); }
place it on the object which has the animation.
That workd but, it stops the animation when I let go of the mouse button, I have to hold it down to play the whole thing, is there anything to fix this?
The commad get button down shold fix it if not let me know
All that does is make it so I have to continue clicking over and over really fast to play the animation, it also stops where I click down, and stops if I hold it, I just gotta keep clicking fast
Yeah, I want it to play the whole entire animation with one mouse button click but for that mouse button down doesn't work like that, it works fine if i basically spam the button though =/
Answer by Remingsworth · Apr 05, 2015 at 02:02 PM
If you want the animation to finish you could try checking your mecanim settings for that particular animation and check [x] 'has exit time'. If that's not then issue then it should be in this segment you posted:
if(!punchAnimation) { _animation = null; Debug.Log("No animations, turning them off");
If I'm reading it correctly it's saying if the condition is not met (mouse has gone up) then animation = null (stopping the animation perhaps?). Hopefully this helps you track the source of the issue so it can be resolved. Cheers.