- Home /
[Solved] Reverse animation
Hello, I am trying to build a simple digger which has a movable digging arm. So far the movement of the arm runs off of an animation which is triggered by the down arrow key. The issue I am running into is that once the animation has reached the end, if I try to move the arm back up with the up arrow key it simply resets the animation to the frame 1 position instead of reversing the animation. If my description isn't very helpful, hopefully you will understand what I am trying to achieve with my code.
Edit: I have managed to work out how to make my animation successfully pause when no keys are pressed, to play when the down key is pressed and to reverse when the up key is pressed. I am posting this code in hope that someone who needs it comes across it in the future.
var Bend_1:Transform;
var Bend1:AnimationState;
var B1A:boolean = false;
var B1B:boolean = false;
function Start(){
Bend1 = Bend_1.animation["Bend_1"];
Bend_1.animation["Bend_1"].wrapMode = WrapMode.ClampForever;
}
function Update(){
if(Input.GetKeyDown("down")){
B1A = true;
}
if(Input.GetKeyUp("down")){
B1A = false;
}
if(Input.GetKeyDown("up")){
B1B = true;
}
if(Input.GetKeyUp("up")){
B1B = false;
}
if(B1A){
Debug.Log("B1A = True");
Bend1.speed = 1;
Bend_1.animation.Play("Bend_1");
if (Bend1.time < 0){
Bend1.time = 0;
}
}
if(B1B){
Debug.Log("B1A = True");
Bend1.speed = -1;
Bend_1.animation.Play("Bend_1");
if (Bend1.time > 1){
Bend1.time = 1;
}
}
if(!B1A && !B1B){
Bend1.speed = 0;
}
}
Answer by Adamcbrz · Jun 19, 2013 at 06:08 PM
Unity3d animation speed is positive only. If you set it to anything below 0 it will assume 0. So you are telling it to play the first frame of the Bend_1 animation. You can do two things here to accomplish your task.
1) Go back to the way you made the animation and create a second animation for the up process.
2) Create a script that will loop through the animation and play the frames in reverse. Something like this might be a good reference http://answers.unity3d.com/questions/32753/ideas-for-animating-while-paused.html
Setting the speed to -1 seems to do the job perfectly fine. I'm assu$$anonymous$$g that when using Wrap$$anonymous$$ode.ClamForever it creates a temporary - value on time as the code in the "if(B1A){" function I used works off detecting if the time is less than 0 which seems to work with no issues.
Your answer
Follow this Question
Related Questions
Can I make animations snap to a frame? 1 Answer
Reversing animation nullpointerexception 0 Answers
Set up Animator with specific seconds 1 Answer
Playing animation in reverse plays it once only! 0 Answers
animation wont reverse 0 Answers