hi folks im stuck now for 3 hours with a really simple issue: i want to play an animation backwards. in the script reference i found that i just need to set the speed to -1. so i did this: Code (csharp): hand.animation["bridge"].speed = -1; hand.animation.Play("bridge"); it does nothing, what am i doing wrong? ps: is there a way to start an animation at a different time in the timeline (ie at the 5. frame)?
I don't see any offending code in your example other than your declaring the hand object before the animation component, you could try adding the animation component to the hand object, making sure you define the animation string name, and then just call animation["bridge"].speed in the code for the object, here is the snippet I use and it works for me: Code (csharp): if(Input.GetKeyDown("w") || Input.GetKeyDown("up")) { animation["walk"].speed = 1.0; animation["run"].speed = 1.0; } if(Input.GetKeyDown("s") || Input.GetKeyDown("down")) { animation["walk"].speed = -1.0; animation["run"].speed = -1.0; } You could run some tests to make sure your playing an animation and it is the "bridge" animation: Code (csharp): if(animation.IsPlaying("bridge")) { print("bridge animation is running"); } Also for the starting the animation at a different timeline, someone can correct me, but I believe you wold use: Code (csharp): animation.Blend("animation",target distance(i.e. 5.0=50% of animation),fadelength(i.e. speed of the aniamtion to blend in)); Hopefully that may help out some.
Hmm, I am wondering how to get this to work: Code (csharp): function Update() { print("Triggerstay= " + triggerStay); if(triggerStay) { if (Input.GetButtonDown ("Fire1") !isOpen) { print("Open door"); deurObject.animation["Open"].speed = 1.0; deurObject.animation.Play("Open"); isOpen = true; } else if (Input.GetButtonDown ("Fire1") isOpen) { print("Close door"); deurObject.animation["Open"].speed = -1.0; deurObject.animation.Play("Open"); isOpen = false; } } } This code uses the "Open" animation to open and close a door. When it's playing forward it animates just fine. But when going backward it closes at once. It looks like the animation simply resets or something. What am I doing wrong?
Sanity11, it's the same problem that I've got. ill try it with rightpurdys method, hopefully that works, thanks!
ok ive put the script directly in the object that has the animation now (had it linked to an empty game object before) and i tried to make some adjustments, but it doesnt change anything. when i print the animation speed, it says -1, but it doesnt play it, instead it immediately switches to the start position of the animation. any1 else who had problems with playing animations backwards? thx in advance ps: it works when i set another positive speed, like 2
I think you need to set when the animation is playing from first. Playing it backwards from 0.0 doesn't do anything. You need to play it backwards from the end of the animation. Code (csharp): hand.animation["bridge"].speed = -1; hand.animation["bridge"].time = hand.animation["bridge"].length; hand.animation.Play("bridge"); Even better is if you can set the wrap mode to ClampForever. Play the animation once at speed 1 and then set the speed to -1 to reverse back to the starting state.
Finally, thank you, setting time to length is something that would ages to think about, here is what I got to: Code (csharp): public void AttackAnimation(bool on){ if(on){ //print ("Attack On"); animation["Attack"].wrapMode=WrapMode.ClampForever; animation["Attack"].speed = 10; animation["Attack"].time = 0; animation.CrossFade("Attack"); }else{ //print ("Attack off"); animation["Attack"].wrapMode=WrapMode.Once; animation["Attack"].speed = -10; animation["Attack"].time = animation["Attack"].length; animation.CrossFade("Attack"); } } public void DefendAnimation(bool on){ if(on){ //print ("Defense On"); animation["Defend"].wrapMode=WrapMode.ClampForever; animation["Defend"].speed = 10; animation["Defend"].time = 0; animation.CrossFade("Defend"); }else{ //print ("Defense off"); animation["Defend"].wrapMode=WrapMode.Once; animation["Defend"].speed = -10; animation["Defend"].time = animation["Defend"].length; animation.CrossFade("Defend"); } } The speed variable is apparently absolute value and not relative, I guess it's a new change along with Mechanim, so I measured it first using Log and then hardcoded like this because using multiplier like: animation[clip].speed*=-1; resulted in oldTime>newTine error or some like that, an alternative could be using absolute math function but for me hardcoding is fine. One last inportant step is the wrapMode, it's imperative that the positive animation has a ClampForever mode while the negative one has a Once mode, otherwise it wouldn't make sense and the animation would be jittery. And last this code is not perfect for long animation because time on a ClampForever animation is not limited to length but infinite which makes impossible to make a smooth animation even with conditional time setting. I hope Unity Team could implement native Reverse Play cuz I thought it was there until I needed it and as far as I know, there is no 3rd party solution as well.
I've got the same problem, and found part of the answer here, thank you! A "play once" animation timer is reset to zero once played. So, try to play it reversed has no effect. You just have to set the timer correctly before playing your animation, this little code works in everycase (forward or backward): if (reversed) { animation[animation_name].normalizedTime = 1f; animation[animation_name].speed = -1f; } else { animation[animation_name].normalizedTime = 0f; animation[animation_name].speed = 1f; } That was the best solution for me, hope it will help
Try this for me worked fine:- if(!animation.IsPlaying("walk")) animation["walk"].time = animation["walk"].length; animation["walk"].speed=-1.0; animation.CrossFade("walk"); Reversing an animation without put the begin at the end make no sense. So if your animation is not playing (before it starts) set the time of the animation to it's end. then set the speed to a negative number and play it.... It works very fine for me.
For those using Unity 5 the following also works. Animator animator; animator = GetComponent<Animator>(); animator.speed =-1; If you have a sliding door blender model with one animation saying opening a door the above can be used to simulate a closing door animation.
why not something like this. Code (CSharp): public string runing; bool run; Animator anim; void Start(){ anim = GetComponent<Animator>(); anim.animation[runing].speed = -1; } void Update () { //input if(run){ //if done run if(anim.animation[runing].time == 0){ anim.animation[runing].speed *= -1; anim.animation[runing].time = Mathf.Clamp(anim.animation[runing].length * -anim.animation[runing].speed,0,anim.animation[runing].length); anim.animation.Play(runing); run = false; } } } its toggleable so it reverses itself each time it is activated.
Unity UI is sort of doing animation forward and backward with Button's. I mean you can apply the same thing to doors, pressureplates and others. You have 2 animations, where both have only 1 keyframe at 0:00. The transition between them is the animation, like ButtonUp -> ButtonDown, and ButtonDown -> ButtonUp. For this we don't need to negate speed.