Play an animation backwards?

I made an animation where my character is pulling out the guns from the holster and when you press the space bar the animation will play

if (Input.GetKey("w"))
		animation.CrossFade ("run");
		else
	if (Input.GetKeyDown("Space"))
		if (GunsDrawn == true)
		animation.CrossFade ("DrawGuns");
		else
	if (GunsDrawn == false)

Now here I want to play the DrawGuns animation in reverse.

I have tried

  animation["DrawGuns"].time = animation["DrawGuns"].length;
		animation["DrawGuns"].speed = -1;

but this just plays the animation in reverse when I press the space bar the second time, it doesnt do anything on the first go.

		animation["DrawGuns"].speed = -1;

This should make it reverse.

if (Input.GetKey("w"))
		animation.CrossFade ("run");
else if (Input.GetKeyDown("Space")) {
	if (GunsDrawn == true)
		animation.CrossFade ("DrawGuns");
	else if (GunsDrawn == false) {
		animation["DrawGuns"].speed = -1;
		animation.CrossFade ("DrawGuns");
        }

}

Maybe this is what you want…

Just throwing it out there, but you could also try to add a new animation in the editor and swap the start and end times of the animation.
ie. if DrawGuns starts at 1 and ends at 200, just flip the 200 and 1 and just name it something else like HolsterGuns

You may also need to set the normalizedTime to 1 (the end of the animation) although I’m unsure if unity will then instantly stop it because it might think it’s finished. If it does change the loopMode to clamp forever.

I am having a very similar problem and I had thought about that about an hour before I ran into the problem and I had forgotten about flipping the animations, THANK YOU!!!

Does that actually work? I never thought to do that.

EDIT: FatFoot, holy shit your avatar is creepy.

I’ve tried this technique several times and haven’t been able to get it to work. But then again I’ve also been unable to make an animation play in reverse by setting the speed attribute through code, so maybe there’s something up with my scene. What’s strange is that I can vary the speed of my animations as long as the value is positive, but negative speed values simply don’t play at all.

Any clues?

More clarity on this topic now:

Duplicating a clip and reversing its start/end times definitely does not work, regardless of the WrapMode. This is unfortunate as it would be a relatively painless thing to do, and would require no code for changing the speed.

Regarding my previous negative speed issue, it turns out that it was working, I just didn’t notice it because it was delayed. This occured because I didn’t properly account for the reversed clip’s length. The clip’s Start=30 and End=120, so I simply set the current time to 3 seconds (120frames - 30frames = 3 seconds), then played it backwards and it ran immediately. For this to work, however, WrapMode must be set to ClampForever.