I’m using Mecanim to handle my animations on my character, which has been great. However, I’ve hit a problem.
I have an attack animation that’s about a second long, and is triggered by a button. In the middle of this animation, if the player presses the same button again, I’d like the attack animation to restart from the beginning. This may be a simple thing, but how might I do that? Mecanim is great at handling transitions from one animation to another, but how would it restart it’s own animation?
Same thing here, did you found any solution ? thanks !
This worked perfectly for me. There was no way I was going to start doubling up on states like the selected answer suggested, so I'm very glad you posted this solution. Thank you! =)
EDIT: This doesn’t work in Unity 5, as mentioned in the comments.
Just a note, at least in Unity 4.1.5, you can use Animator.ForceStateNormalizedTime which takes in a normalized float (0.0 to 1.0 range) to force the playback position of the currently active state (supposedly won’t work while in transition).
So you can do something like myAnimator.ForceStateNormalizedTime(0.0f) to replay the current animation.
This stopped about two hours of headache over animation - this should be more... normal... where in the F is Animator.ResetAnimation(); at??? Thumbs up to you good sir! Hope it doesn't get phased out in 5x - I see it is already marked as deprecated!
Please note this is no longer valid. You have to set the play time when calling AnimatorRef.Play("state", layer, playbackTime). See @komodor's answer for more details.
Not totally sure but it sounds like someone from Unity said: “We wont support API control over transition in 4.0. So only in the UI… for now”, so a quick way if it isn’t doable in the code that you can find, you can create a state in the UI and have it transition to the same animation (and maybe back again?) if you set a certain flag.
sure it work, but it's pretty dirty solution, imagine you have many states and you need all of them doubled ... the Play(state, layer, normalizedTime); works same way but it's cleaner
If you’re trying to accomplish something like this from the animation controller state machine instead of from code, you’re going to want to uncheckHas Exit Time and check Can Transition to Self on the transition from Any State to your state.
Same thing here, did you found any solution ? thanks !
– anthonyk2