- Home /
Animation.Rewind Isn't Working
Hello Everyone,
I have an animation clip that opens a door. It works great. I'm having trouble closing the door. Instead of creating an animation clip that closes the door I noticed there's an animation.rewind command. It isn't working and I was hoping one of you fine ladies/gentlemen could help me out.
Here's my code:
public class OpenDoorTrigger : MonoBehaviour {
public bool triggered = false;
void Update(){
if (Input.GetKeyDown(KeyCode.E) && triggered == true){
Debug.Log("EEEEE");
animation.Play("OpenDoor");
}
else if (Input.GetKeyDown(KeyCode.E) && triggered == false && !animation.isPlaying){
Debug.Log("REWIND");
animation.Rewind("OpenDoor");
}
}
I did a Debug.Log test and it does Log 'Rewind' so animation.Rewind("OpenDoor"); isn't working. Does anyone know why?
I'd appreciate any feedback. Thank you for your time and have a nice day.
Just a guess since I never used animation.Rewind, but you might need to stop the OpenDoor animation before it actually completes.
I'm not sure if rewind is supposed to actually play it back normally or simply jump it back to the start?
I think you would be much better off just creating a new animation for the door to close it, I think things could get a bit messy with the Rewind stuff but once again I never used it so just speculating.
I think you might want to just set the animation speed to being negative ins$$anonymous$$d of rewind though if you are deter$$anonymous$$ed to not just create a new "CloseDoor" animation.
Here's a link about that:
http://answers.unity3d.com/questions/16198/animation-reverse-for-movement.html
Answer by Baroque · Feb 28, 2014 at 04:56 AM
Animation.Rewind
just sets the animation back to frame zero. You want to use play, but set the playback rate to be negative to play the animation in reverse:
void Update()
{
AnimationState state = animation["OpenDoor"];
if (Input.GetKeyDown(KeyCode.E) && triggered == true){
Debug.Log("EEEEE");
state.speed = 1.0;
animation.Play("OpenDoor");
}
else if (Input.GetKeyDown(KeyCode.E) && triggered == false && !animation.isPlaying){
Debug.Log("REWIND");
animation.Play("OpenDoor");
state.speed = -1.0;
state.time = state.length;
}
}
Even after over 8 years, your solution was extremely helpful. Thanks!