- Home /
Looping animation while key down
Heres animation trigger script
function Update () {
if(Input.GetKeyDown("f")) {
animation.Play("Gun and run");
}
}
I want to set it so if i hold down f the animation loops and when i let go of f the animation stops
How about adding an else to that if. ;-)
else if(animation.isPlaying("Gun and run")){
animation.Stop("Gun and run");
}
Also have a look at additive animations and animations in layers (where you'd be able to combine two animations of gunning and running at the same time).
No your not gunning and running i was just bored and named it that its just one simple animation of your arms moving.
I stand corrected. Although, giving things semantic names will help you better here in the future (perhaps not in this case).
Accidently had a capitalized I on is, try either without the complete "else if"-statement and just go with "else" or switching I to i on isPlaying. Always look in the console for hints of errorneous script.
Answer by Joshua · Jun 21, 2011 at 03:01 AM
function Update () {
if(Input.GetKeyDown("f")) {
animation.Play("Gun and run");
}
if(Input.GetKeyUp("f")) {
animation.Stop("Gun and run");
}
}
note that the answer provided in the comments is incorrect since GetKeyDown will only return true for the frame the key is pressed, not for the frames afterwards when it's kept down. It would work if he'd had used GetKey