I am animating my character by using animator with gameObject. my animator has multiple animation clips(states) attached with each other through transition. my character is animating against the mouse event, which run all animator states one by one.
now i want to reverse animate these clips(from last frame to first frame) against another mouse event but my code is not working correctly.
but Animator does not has property “time” and i can not access my animation clips as accessed here. i got the exception
“MissingComponentException: There is no ‘Animation’ attached to the “D_character” game object, but a script is trying to access it.
You probably need to add a Animation to the game object “D_character”. Or your script needs to check if the component is attached before using it.”
image of the animator states is below.
i have added the condition for reverse transitions, they will run only when parameter “Reverse” is true.
Yeah, time is not revealed in the Animator component. I hope they will in the future as i need to slow down animations and reverse them my self, so i'm still using the legacy Animation instead of Animator. They said that Animation will go away eventually so perhaps they'll put time in there. Only thing i can think of is to make a different animation that is the reverse of that and use that one.
Just copy and paste the Animation state that you want to reverse on the Animator Window, then rename it and set the speed that you want.( -1 for reverse). After that you just call the animation by the new name.
Hello, can you post a code example of this please? I tried to do this like so: myGameObject..GetComponent<Animator> ().Play("reverse"); But, it did not work :(
You can make a animation loop or not by setting the loop checkbox on the animation file through the inspector… as long as the state is on… it will keep looping or do one pass… forwards and backwards with the animator speed variable. So you would only need one state/animation for moving and another for not. You may want to organize and rename you States a little bit.
I also optimized and cleaned up your script a bit.
public Animator anim; //Set me in inspector! Always try to avoid GetComponent... Horrible little method function that one.
public bool forward;//So you can see in the inspector or change it through a animation or script.
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
forward = !forward;//Toggles it between on or off... You are welcome if you weren't aware of this syntax.
}
anim.speed = (forward) ? 1 : -1;//Conditional assignment... look it up.
anim.SetBool("Reverse", !forward );//Reverse is the opposite of forward... Right? If not... I'm missing the desired end result.
anim.SetFloat("Speed",anim.speed);//I think you wanted this earlier so you can find out the speed. No?
}
Also this might help you more or less… If anything, Something I gave helped somebody.
I will revisit my answer if you need more help or you don’t get something.
Animations can be played backward in 2021 by setting a float or int value parameter in the animator and assigning it to the multiplier in the animation.
I have figured out the problem that we cannot play animation in reverse by setting the value of animator.speed as -1 in code, as i am using unity’s new component Animator not Animations so i created duplicate animation clips(states) with opposite transitions and set there speed property in Inspector as -1 and call them when i want to show reverse animation.
@diliupg
how do you assign the multiplier in the animator?
Edit:
Nevermind, I just used some blend 2D parameters in my case for a 2D animation, since that reverse animation script wasnt working
Yeah, time is not revealed in the Animator component. I hope they will in the future as i need to slow down animations and reverse them my self, so i'm still using the legacy Animation instead of Animator. They said that Animation will go away eventually so perhaps they'll put time in there. Only thing i can think of is to make a different animation that is the reverse of that and use that one.
– screenname_taken