Animation Script help!

Hello! So I have this script here,

var resetTime = 0f;
var isReset = false;


function Update ()
{
   if (Input.GetKey   ("s")) {
      animation.Play("Slide");
      isReset = true;
    }


if(isReset){
if(resetTime<=3){
resetTime+=Time.deltaTime;
}
if(resetTime>3){
animation.Stop("Slide");
isReset = false;
resetTime = 0;
}
}



   }

And it’s supposed to play an animation when I press the S button, it works really well. But the problem is that I need it to play for about 2-3 seconds, and right now it only plays as long as you hold the button down. Is there any way to do this? I know in C# there is a sort of wait for ____ seconds command. Is there any way to do that in Java? Thanks!

-Rov

Rovalin, It would really help if you would format your code. Please use the “Code” button in the future.

First, GetKey listens for continuous firing of the key. If you want to hit S and it plays an animation for 2 seconds, use GetKeyDown instead of GetKey. It will only fire when the key is first pressed.

So it would be something like

var canAnimate : bool = false;
function Update(){
     if(Input.GetKeyDown("s")){
          RunAnimation();
     }
     if(canAnimate){
     animation.Play("Slide");
     }

}
function RunAnimation(){
     canAnimate = true;
     yield WaitForSeconds(2);
     canAnimate = false;
}

I did not test or Debug the script, this is just to give you the idea.

if you want to prevent someone from spamming S while the animation is already running change the input thingy to

if(Input.GetKeyDown("s") && canAnimate = false){

and if your wrapmode is set to loop or something. ad the else stop to the play command.

if(canAnimate){
   animation.Play("Slide");
}
else
    animation.Stop();