Reverse Animation by using Animator.

I am animating my character by using animator with gameObject. my animator has multiple animation clips(states) attached with each other through transition. my character is animating against the mouse event, which run all animator states one by one.

now i want to reverse animate these clips(from last frame to first frame) against another mouse event but my code is not working correctly.

here is my code.

void Start ()
		{
			anim = GetComponent<Animator>();
		}
		
		void Update ()
		{
			if (Input.GetMouseButtonDown (0)) {

					if (forward) {

							forward = false;
							anim.speed = 1;
							anim.SetBool("Reverse", false );
							anim.Play("D_Animation");

					} else {

							forward = true;
							anim.speed = -1;
							anim.SetBool("Reverse", true );
							anim.Play("D_Animation3");
					}
			}	
		}

i have searched for this issue and i found the solution

            animation["AnimationName"].time =animation["AnimationName"].length;
	animation["AnimationName"].speed = -1;
	animation.Play("AnimationName");

but Animator does not has property “time” and i can not access my animation clips as accessed here. i got the exception

“MissingComponentException: There is no ‘Animation’ attached to the “D_character” game object, but a script is trying to access it.
You probably need to add a Animation to the game object “D_character”. Or your script needs to check if the component is attached before using it.”

image of the animator states is below.

i have added the condition for reverse transitions, they will run only when parameter “Reverse” is true.

[25854-screen+shot+2014-04-28+at+12.47.48+pm.png|25854]

Just copy and paste the Animation state that you want to reverse on the Animator Window, then rename it and set the speed that you want.( -1 for reverse). After that you just call the animation by the new name.

You can make a animation loop or not by setting the loop checkbox on the animation file through the inspector… as long as the state is on… it will keep looping or do one pass… forwards and backwards with the animator speed variable. So you would only need one state/animation for moving and another for not. You may want to organize and rename you States a little bit.
I also optimized and cleaned up your script a bit.

    public Animator anim; //Set me in inspector! Always try to avoid GetComponent... Horrible little method function that one.
    public bool forward;//So you can see in the inspector or change it through a animation or script.
    void Update ()
           {
             if (Input.GetMouseButtonDown (0)) {
                       forward = !forward;//Toggles it between on or off... You are welcome if you weren't aware of this syntax.
             }
                       anim.speed = (forward) ? 1 : -1;//Conditional assignment... look it up.
                       anim.SetBool("Reverse", !forward );//Reverse is the opposite of forward... Right? If not... I'm missing the desired end result.
                       anim.SetFloat("Speed",anim.speed);//I think you wanted this earlier so you can find out the speed. No?
           }    

Also this might help you more or less… If anything, Something I gave helped somebody. :smiley:

I will revisit my answer if you need more help or you don’t get something.

I have figured out the problem that we cannot play animation in reverse by setting the value of animator.speed as -1 in code, as i am using unity’s new component Animator not Animations so i created duplicate animation clips(states) with opposite transitions and set there speed property in Inspector as -1 and call them when i want to show reverse animation.

bool forward = true;
    	private Animator anim;
    	
    	void Start ()
    	{
    		anim = GetComponent<Animator>();
    
    	}
    	
    	void Update ()
    	{
    		if (Input.GetMouseButtonDown (0)) {
    			if (forward){
    
    				anim.Play("firstAnimation");
    
    			} else {
    
    				anim.Play("secondAnimation_R");
    
    			}
    			forward = !forward;
    		}
    	}

[26159-screen+shot1.png|26159]

Animations can be played backward in 2021 by setting a float or int value parameter in the animator and assigning it to the multiplier in the animation.