- Home /
Unity 5. Reverse animation play ?
Hello. How do i play animation in reverse? i been searching in internet and i found this:
animation["Zoom"].speed = -1;
but its outdated now or i am doing this wrong. How do i play animation in reverse using unity 5? i start animation like this:
bonuspanel.GetComponent<Animation> ().Play ();
soo it should be the same just like this:
bonuspanel.GetComponent<Animation> ().speed = -1;
But speed does not exist. I hope someone can help me :)
Answer by HarshadK · May 12, 2015 at 08:38 AM
The solution by careyagimon is in this forum thread playing animation backwards.
It states:
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.
Like this? :
using UnityEngine;
using System.Collections;
public class HideBonusPanelAnimStart : $$anonymous$$onoBehaviour {
public GameObject showbonuspanelbutton;
public GameObject bonuspanel;
public void hidebonuspanel(){
if (transform.position.y == 90) {
bonuspanel.GetComponent<Animation> ().speed = -1;
bonuspanel.GetComponent<Animation> ().Play ();
}
}
}
speed does not exist i think i need to set what it is.
speed and time are the variables from AnimationState so you need to set them for specific animation state for which you want to change these values.
In example code above in the answer "bridge" is the name of the animation clip. So you need to do something like:
Animation animation = bonuspanel.GetComponent<Animation>();
animation["YourClipName"].speed = -1;
animation["YourClipName"].time = animation["brYourClipNamedge"].length;
animation.Play("YourClipName");
Like this? :
using UnityEngine;
using System.Collections;
public class HideBonusPanelAnimStart : $$anonymous$$onoBehaviour {
public GameObject showbonuspanelbutton;
public GameObject bonuspanel;
Animation animationbonuspanel;
void Start(){
animationbonuspanel = bonuspanel.GetComponent<Animation> ();
}
public void hidebonuspanel(){
if (transform.position.y == 90) {
animationbonuspanel["BonusPanel"].speed = -1;
animationbonuspanel.Play("BonusPanel");
}
}
}
It does not work
Also, don't forget to set things back to normal in order to play the animation forward again, ie:
animation["YourClipName"].speed = 1;
animation["YourClipName"].time = 0;
You are forgetting this line:
animation["YourClipName"].time = animation["YourClipName"].length;
You need to call this line before calling the Play on your animation.
This line will set your animation to its last frame so that you can run it in reverse.
Answer by brainpower · Feb 21, 2016 at 04:27 PM
Despite what Jasper_Moore said about speed multipliers, they worked for me.
Here's what I did:
1) Add a float parameter to the Animator (I called it "Direction"). Initialize its value to 1.0.
2) In the relevant animator state (in my case "Walking"), link the float parameter to the speed multiplier, like so:
3) In your script, set the float parameter to -1.0 (for backwards) or 1.0 (for forwards), like this:
animator.SetFloat("Direction", -1.0f);
This Doesn't work in unity 2017.2, $$anonymous$$aybe there is some bug.
This is the correct answer. Unity state machine was designed for parameters and having a parameter that you set of -1.0f or 1.0f to control the direction is the correct way to do this. Works perfectly.
If you are ins$$anonymous$$d already using a parameter for the multiplier then you can control it by making that speed negative as well.
Answer by Jasper_Moore · Aug 20, 2015 at 11:16 AM
While it would make sense to simply change the playback speed, or to create a float value in the animator for a speed multiplier, I have found it to be very inconsistent and buggy, if it works at all. The only consistent method I've found is stepping through the animation manually, like so:
public Animator myAnim; //Animation controller, assign in inspector.
private float animTime; //Tells us where we are in the animation's timeline.
private bool anim_Play; //Should the animation play?
void Update() {
if (anim_Play && animTime < 1) //If animation is toggled on
animTime += 0.3f * Time.deltaTime; //Increase animTime, Don't let animTime go beyond 1
else if (!anim_Play && animTime > 0) //If animation is toggled off/reversed
animTime -= 0.3f * Time.deltaTime; //Decrease animTime, Don't let animTime go below 0
myAnim.Play("Some_Animation", 0, animTime ); //Step through animation manually
}
//Call this function to toggle forward/reverse. public void ToggleAnim() {
anim_Play = !anim_Play;
}
This is a simple setup for a single animation (in my case, a door opening/closing). The animation will automatically idle at beginning and end. If you want it to do something else after playing, you'll have to experiment to work that out. Also, I used 0.3 for the speed multiplier, this can be adjusted to fit your needs. Hope this helps anyone having this problem!
Just to be clear, Here is what my animator view looks like for this setup:
Answer by yankun1997 · May 05, 2017 at 09:34 AM
This is how i do,mine is quite straightforward
private animatior anim;
private float direction;
void update () {
if (Input.GetKeyDown("space"))
{
anim.SetFloat("Direction", -1);
anim.Play("WAIT02", -1, 0f);
}
}
and you must set a float parameter in the animator (a transition) , in my case it's named direction.
hope it works for you