- Home /
When pressing a key, play an animation, when pressing same key, play different animation?
I need to play an animation every time you press the 's' key, but I need to play 4 animations using the same 's' key. How can you do this? This is what I have so far:
function Update()
{
if (Input.GetKeyDown("s"))
{
animation.Play();
}
}
This works fine, but I need it so I can play three more animations using the same key. How?
Thanks guys, any help is appreciated
Are you trying to randomize the animations? Or must they play in a particular order? I'm not quite understanding what you are trying to do.
Answer by Linus · Jul 07, 2012 at 06:33 AM
Have a variable that is set to a value depending on how many times the key has been pressed. Think an into should work in this example.
var currentState : int = 0;
//if (Input.GetKeyDown("s"))
if(currentState == 0){
animation.Play(); //Set to correct annimation
currentState = 1;
} else if(currentState == 1){
animation.Play();
currentState = 2;
}else if(currentState == 2){
animation.Play();
currentState = 3;
}else if(currentState == 3){
animation.Play();
currentState = 0;
}
This code is demonstration only. But I think you get the idea.
Great, if you feel it was what you where looking for. Please mark as answered
Your answer
![](https://koobas.hobune.stream/wayback/20220613073821im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Update iTween Path at Runtime 1 Answer
Animation within Unity 1 Answer
Animation by Scripting point A to B 1 Answer