- Home /
pingpong animation question
Hello,
How do I get an animation to play all the way through to the end while a key is pressed, and then after the key is released, rewind back to the beginning?
Answer by Scribe · Jun 08, 2012 at 03:01 PM
I haven't tested this so it may be completely wrong however I would guess you could do:
animation.Play("ClipNameHere");
if (Input.GetKey ("space")){
animation["ClipNameHere"].speed = 1;
}else{
animation["ClipNameHere"].speed = -1;
}
sorry if this isn't correct! As I said this isn't tested and I haven't used many animations lately :)
Scribe
Why did my answer get voted down :( I tested it and it very almost works apart from at the end of the animation it restarts but that can be fixed probably by setting the animation to 'clamp forever'? could someone correct me if I'm wrong or give me a reason for the down vote please! :)
I have no idea why it got downvoted. It's actually a very good approach. The only thing that's missing is: - Don't use wrapmode "Once" because it will auto-rewind the animation when it's done. Use ClampForever. - The animation time can go beyond the clips borders (smaller than 0 and greater than cliplength). You need to clamp the time between the borders. This can be done with the normalized time.
So this does work pretty well with wrapmode ClampForever:
animation.Play("ClipNameHere");
if (Input.Get$$anonymous$$ey ("space")){
animation["ClipNameHere"].speed = 1;
}else{
animation["ClipNameHere"].speed = -1;
}
animation["ClipNameHere"].normalizedTime = $$anonymous$$athf.Clamp01(animation["ClipNameHere"].normalizedTime );
Btw, this will also reverse the direction while the animation is playing. So if you release the key after 1/3 of the clip it just animates backwards the first 1/3
Here i've created a sample build which shows how it works.
Thanks @Bunny83 I thought I might have gone mad! :) Yeah I found I had the problem that it repeated itself, would setting the wrap mode to clampforever not work? or do you do it this way because it has better performance as I've heard clampforever isn't very good for performance?
Answer by commodore · Jun 10, 2012 at 05:20 AM
Bunny your sample project is exactly what I needed! thank you
Your answer
Follow this Question
Related Questions
ANimation doesn't rewind on animation.Stop 3 Answers
Can the animation editor create local rotational data? 3 Answers
How do you rewind an animation that isn't playing? 2 Answers
Adding animation clips via script 2 Answers
Rewind/Play animations on demand. 1 Answer