[Solved] Reverse animation help

Hello, I would like to know how I can play an animation backwards. I tried with animation.time = animation.length, or something like this, but it played the animation only once. How can I play it continuously while pressing “S” ?

I’m not entirely sure what exactly you want but maybe it’s this ?

https://dl.dropboxusercontent.com/u/7761356/UnityAnswers/Web/PingPongAnimation/WebPlayer.html

You need to set the animations wrapmode to ClampForever or the animation will stop at the end and need to be restarted.

edit

Here’s an editor script i’ve written to reverse an AnimationClip. Since it’s an editor script you have to put it in a folder called “Editor”. Once it’s there a new mainmenu item called “Tools” will appear (if not just click the “File” menu once).

Now you have to duplicate the AnimationClip in Unity by pressing CTRL+D while you have selected the AnimationClip of you character. Select the duplicated Clip and click on Tools/ReverseAnimation. This will reverse the animation. Now you can add this new animation to your Animation component of your character as seperate animation by increasing the animations-array size by one and assigning the animationclip to the new slot. Keep in mind to give your AnimationClip a meaningful name :wink:

//ReverseAnimation.cs
using UnityEngine;
using UnityEditor;
using System.Collections;

public class ReverseAnimation : Editor
{
	public static AnimationClip GetSelectedClip()
	{
		var clips = Selection.GetFiltered(typeof(AnimationClip),SelectionMode.Assets);
		if (clips.Length > 0)
		{
			return clips[0] as AnimationClip;
		}
		return null;
	}
	[MenuItem("Tools/ReverseAnimation")]
	public static void Reverse()
	{
		var clip = GetSelectedClip();
		if (clip == null)
			return;
		float clipLength = clip.length;
		var curves = AnimationUtility.GetAllCurves(clip,true);
		clip.ClearCurves();
		foreach(AnimationClipCurveData curve in curves)
		{
			var keys = curve.curve.keys;
			int keyCount = keys.Length;
			var postWrapmode = curve.curve.postWrapMode;
			curve.curve.postWrapMode = curve.curve.preWrapMode;
			curve.curve.preWrapMode = postWrapmode;
			for(int i = 0; i < keyCount; i++ )
			{
				Keyframe K = keys*;*
  •  		K.time = clipLength - K.time;*
    
  •  		var tmp = -K.inTangent;*
    
  •  		K.inTangent = -K.outTangent;*
    
  •  		K.outTangent = tmp;*
    

_ keys = K;_
* }*
* curve.curve.keys = keys;*
* clip.SetCurve(curve.path,curve.type,curve.propertyName,curve.curve);*
* }*
* var events = AnimationUtility.GetAnimationEvents(clip);*
* if (events.Length > 0)*
* {*
* for (int i = 0; i < events.Length; i++)*
* {*
events_.time = clipLength - events*.time;
}
AnimationUtility.SetAnimationEvents(clip,events);
}
Debug.Log(“Animation reversed!”);
}
}*_

I had a hard time figuring this out, too, since most of the documentation states to set animation.speed to -1. What I found works is to negate the animation and set the time to the end of the animation: animation.speed = -animation.speed; animation.time = animation.length