How to use PingPong animation without repeatation??

i want to open and close a door with same button... i use PingPong but when i press key to open it.. its starts repeating the animation... and if i use once it do it just one time and after pressing againg it again open the door

i just want to use same open animation in reverse...with same key what do to. ??

2 Answers

2

Do This: http://answers.unity3d.com/questions/45189/reversing-door-animation

var open : boolean = false;
Update() {
 if (Input.GetKeyUp("k")) {
   open = !open;
   if (open)
     .. play forward .. 
   else
     .. play backward ..
  }
} 

but how to do this with the same key to open it and close it?

Look for Input.GetKeyUp("k") and have a boolean called 'open' that is toggled, somethng like the above

You could of course also look for GetKeyDown ;)

ok ..but still animation is not working in reverse...

use a negative speed to play the animation backwards, you shouldn't use pingpong for this. animationState.speed = -1.0f; //<- like this

For those searching for a solution to use PingPong only once, without repeating, I have seperate solution post here:

Ping Pong Only Once

In terms of playing an animation just forward, then just backwards on a key stroke, which seems to be the actual intent of the query in this posting, here is a complete C# implementation derived from DaveA’s clever answer above.

bool isOpen = false;

void Update() {

    if (Input.GetKeyDown(KeyCode.K))
    {
        isOpen = !isOpen;
        if (isOpen )
        {
            animation["open"].time = 0;
            animation["open"].speed = 1;
        }
        else
        {   
            animation["open"].time = animation["open"].length; // Start at the end...
            animation["open"].speed = -1; // ...and run backwards!
        }
        
        animation.Play("open");
    } 
}