- Home /
How do I stop animation from going past end time
This is similar to this unanswered question https://answers.unity.com/questions/1050555/how-to-stop-animation-from-going-past-the-last-fra.html
I have an animation that I want to play forward when the user presses the [left arrow key] and play backwards when the user pressed the [right arrow key]. This works fine by changing the speed multiplier of the animation between 1 and -1, however, it seems the animation time continues so if the user presses one way and then waits and then presses the other way the animation is not visible for some time as the animation time is outside that of the actual animation.
Answer by Przemo0c · Nov 16, 2017 at 09:10 AM
Okay I have heard about the speed multiplayer of "-1" way of doing that, but I also did tried it and it did not work for me in 100% of the times. This is going to be a lesson, a bit long one but it works 100% Personally I would do something like that : if you're using Animator Component, Click to create new animation (Like here) Then record your animation as you want it to be, then create second animation inside the same Animator > choose dropdown menu > create Animation > Then Open Animator Layers (You should have sth like this) And then you will have to create new Panel (So the window looks like the one I have showed you) > click on the grid > Create State > Empty > Name it as you want (I named it Start - This is the Yellow Panel ) then click on your created Panel > Set as Layer Default State and then > Click Again on your Panel > Make Transition > Connect it with your first (in your playing order) Animation > Then From your first Animation Click again > Make Transition > Connect it with your second Animation > Then Connect your second animation back to the panel you created and set to default. (So it looks like the image I have provided). Now we wanna go to creating parameters, you need to find the tab parameters that is on the left (Look at second screenshot) click that "+" icon > Bool > Name It as you want. Now we're going to be clicking on those arrows you have created as transitions (starting from the first gray one), if you click on it in Inspector tab you will have section "Conditions" and again click there "+ " icon > Choose your parameter set it to depending on the logic you are using false/true. My transition arrow condition since my parameter is named "_isVisible " order (starting from Start) is true > false > false.
And now the easiest part if you want your animation to play via coding : Mine looks like this, this one shows the panel :
panel.GetComponent<Animator>().SetBool("_isVisible", true);
so general idea would be :
[YourGameObject].GetComponent<Animator>().SetBool("[name of your parameter]", true);
Anyways I know it is a lot to read and learn, but I can assure you that it is going to work, if you have any questions go ahead and ask :P