Hi there,
Is there a way to play an animation in reverse midway without setting the Animator’s speed?
I’ve seen people suggest setting the speed of the Animator, but the Animator is controlling all the animations on that object, monitoring when that should be playing normally or not becomes too complicated.
The speed of an animation should be able to be set on the animation itself either by code or not, which it can be, by the multiplier (which also seems to then ignore exit time - Speed multiplier ignores exit time - Unity Engine - Unity Discussions), however this will restart the animation each time it needs to play backwards.
I want to play an animation provided a condition is met, however if during the animation it is no longer met, I want to reverse that animation from where it currently is.
All help appreciated!
Have you tried setting it in code? I’ve found that changing properties in the animator window will reset the animation, but that doesn’t happen when setting it with animator.SetFloat.
Hey thanks for the response. Yeah I’m currently only setting it in code:
bool isMouseDown = Input.GetMouseButton(1);
anim.SetInteger(AIM_SPEED, isMouseDown ? 1 : -1);
The problem is it jumps to the beginning of the animation to then rewind itself.
Try a float instead of an int? It works for me without resetting the animation if I do that.
As in it plays backwards for you? Could you share your code and a screenshot of your animator?
Nothing special:
float speed = animator.GetFloat("Animation speed");
speed = EditorGUILayout.Slider("Animation speed", speed, -1, 1);
animator.SetFloat("Animation speed", speed);
Ah ok but you’ve only got the one animation. If you want to transition to another state depending on whether speed is greater than or lower than 0, does that work for you?
Can you make a sample scene to show what you’re doing (so I don’t have to guess ?
Haha of course! Would’ve been smarter from the start, hehe:
https://www.dropbox.com/s/xmq4qkiydasqq8g/Animation%20Issue%20Example.zip?dl=1
So in the example, if you hold right click the stick should animate down, then once the animation is complete animate side to side. Once you let go of right click, the stick should animate up and then return to idle.
The issue is two fold:
- The stick animation has to complete, you can’t let go half way through and it animate from where it was.
- Secondly, the middle state (“Animate Out”) uses a multiplier on it to determine whether it should the animation should play forwards or backwards, and it seems like the animation never finishes when it goes backwards, so on returning to idle, it gets stuck in the middle state.
Thanks so much for your help!
The animation doesn’t restart for me (if I click and release during the “Out” transition, it goes up and down without restarting), but the transition never triggers if the speed is negative (in 2017.1.0f3) If I turn off Has Exit Time, the transition triggers, of course at the wrong time, without waiting.
They’re probably doing something like “if(new time >= exit time && old time < exit time)”, which assumes time is moving forwards. What they should be doing (if my guess is correct) is more like “if(new time >= exit time != old time < exit time)”, which detects crossing the exit time in both directions. But we can’t fix that (might want to file a bug if you think that’s what’s happening), so let’s look for a workaround…
You could make separate reversed copies of each clip. Then you could switch from the forwards animation to the corresponding backwards animation when you need to switch direction, and you’d never have to use a negative speed. If you’ll have a lot of animations this could be a pain, but you could probably automate creating the reversed animations with an editor script…
Yes I ended up finding the same thing. But what I want is to be able to click and release instantly, and it return to idle from the current animating out frame, rather than having to go all of the way up, and all of the way down. I can’t seem to find a way to achieve this, it has to go all the way up, and then realise it should be down and return to down.
I have done the separate reversed clips, which does make it work with up and down, however I seem to be then be even further away from being able to lower mid-raise, as they’re two entirely different clips?
You’d need to switch between the clips manually, by looking at the current time of the animation and matching it up with the corresponding time in the other animation. For example, in normalized (0-1) time, if you’re at 0.25 in the “down” animation, that’s equivalent to 0.75 in the reversed “up” animation, so you’d call Play(“UpAnimation”, time) to play that animation.
You can query the current time of the animation with Animator.GetCurrentAnimatorStateInfo().normalizedTime, to figure out what time you need to switch to, and you can get the currently playing clip with Animator.GetCurrentAnimatorClipInfo, so you can figure out which clip to switch to (eg. if you’re on “Up”, match it with “Down”).
Very awkward, for sure–maybe somebody has a better idea–but it’s all I can think of. Be sure to file a bug about transitions not working when playing backwards, since getting that fixed would make this a lot easier for you…
Hmm. While looking at animations for UI (very not sold on this…), I noticed something else strange. If I set the speed (not speed multiplier) of a state to -1, the clip plays backwards, but the blue bar in the animator view moves forwards, which means normalized time is still moving in a positive direction. In this case, transitions work, but to have a transition play when the reversed clip finishes, I have to set its exit time to 1, not 0.
In contrast, setting the speed multiplier to -1 causes the blue bar in the animator view to move backwards, and exit time transitions don’t work at all.
Doesn’t help the original problem, just some more weirdness that seems worth mentioning. You’d expect a speed multiplier to have the same effect as, well, multiplying the speed, but it actually behaves very differently.
Hi guys, I’m currently working on this bug but haven’t found a way to solve the issue for exit time with speed multiplier.
Effectively setting the state speed to -1 reverse the whole clip. So you still see the blue bar going forwards.
But I’m still not sure how to deal with speed multiplier and Exit time.
Here some detail
An exit time condition is true only if the previous state time is before exit time and current state time is after exit time
previousTime < ExitTime && currentTime >= ExitTime
this work well if the time is going forwards.
But with speed multiplier the time can switch from forwards to backwards, in this case previous time is always bigger than current time, so the condition for exit time will never be true
@gumboots is expecting that a transition with an exit time of 1 should be triggerred either if the time is going forwards or backwards. But if you start to play a clip with speed multiplier of 1 and then half way in the middle of the clip you change the speed multiplier to -1 you will never get to the end of the clip.
One thing that could maybe work but still need a patch in the code is a transition with an exit time of 1 and speedMultiplier > 0 for forwards playing and another transition with an exit time of 0 and speedMultiplier < 0 for backwards playing.
To make it work I would need to reverse the previous and current time when we detect a negative speedmultiplier.
What do you think? or maybe you have another idea?
That’s what I suggested was happening above (“if(new time >= exit time && old time < exit time)”). A crossed check would handle both directions (being careful not to be off-by-one when going backwards, which my earlier suggestion was):
(previousTime < ExitTime && currentTime >= ExitTime) ||
(previousTime > ExitTime && currentTime <= ExitTime)
This doesn’t care which direction time is moving, or if the play direction is changing dynamically.
@Mecanim-Dev The only thing is ideally you could keep an exit time of 1 to allow for interrupting an animation. So a normalised exit time of 1 means ‘the end’, it doesn’t care whether that’s the start or the end, and that’s dictated by the final calculated animation speed. Like you said, if the speed was changed part way through you’d need to recalculate the current time position. But that should solve the issue, right?
It won’t work for all case. Like I said if you start to play the clip, then half way you change the speed multiplier from positive to negative, the time will start to go backward so what would be the end of the clip in this case?
Also we need to handle case with transition that have an offset, there is nothing that could prevent someone to transition at 50% of the clip, in this case what would be the end of the clip? it not clear.
In your particular case that would work because the motion is symetric, the motion stop where it started, but it not always the case
So we won’t change the meaning of exit time, 0 mean begining of the clip, 1 mean end of the clip
The change that we propose only affect how the condition for exit time is handled.
So in your case you will still needs two transition
AnimateOut → Animate Sideways: exit time=1 && AimSpeed > 0
AnimateOut → Test Animation: exit time=0 && AimSpeed < 0
1 Like
@Mecanim-Dev
Hello, have this thing been implemented already in new versions? I need it now)
yes it should work now in 2018.1
4 Likes