- Home /
Controlling animation speed
Greetings! I've got such a question:
I want to control my animation movement speed by pressing a key on a keyboard. For example, when the specified key is held, animation plays (play speed 1.0), but when I release the key it should stop (play speed 0.0). And when I will press the key again it continues from the point where it was stopped.
I'm trying to deal with it in this way:
function Update (){
if(Input.GetKey("page up"))
animation.Play("animation_name");
animation["animation_name"].speed= 1.0;
else
animation["animation_name"].speed = 0.0;
}
It doesn't work, saying: BCE0044: expecting }, found 'else and something like that. I know that I do it wrong, but don't know how will be correct. I wish if only you could help me with this task. Just some advices. PS: I have read practically every topic concerning this question, but still don't understand.
Answer by clunk47 · Oct 10, 2013 at 07:21 PM
The error is because you have more than one command in your if statement, but didn't open / close the if statement with brackets. Go from there. Also check to see if your animation is already playing or not before you play it, so it doesn't start over each time.
#pragma strict
function Update ()
{
if(Input.GetKey("page up"))
{
if(!animation.isPlaying)
animation.Play("animation_name");
//This isn't part of your if(!animation.isPlaying) condition.
animation["animation_name"].speed= 1.0;
}
else
animation["animation_name"].speed = 0.0;
}
Please refrain from stacking questions, ask you new question as that, a new question so that others have a chance to help, and others will be able to more easily find information on this new issue on UA.
If this resolved your first issue, as a user you are required to Accept the answer (check mark). And, not required, but you can also upvote answers or comments that are helpful to you (thumbs up).
Normally the negative form should reverse the animation speed and thus the animation itself.
And if it really does not work, simply make a revers going animation and insert it in a separate slot.
Answer by austintone · Jan 07, 2017 at 08:21 PM
you're missing the bracket before and after the 'if' statement begins and ends. You have to end the 'if' statement before you start the 'else' statement. It should be this:
function Update (){
if(Input.GetKey("page up")) {
animation.Play("animation_name");
animation["animation_name"].speed= 1.0;
}
else {
animation["animation_name"].speed = 0.0;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613115710im_/https://answers.unity.com/themes/thub/images/avi.jpg)