Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Screenhog · Feb 01, 2013 at 06:43 AM · animationmecanimrestartreplay

Restart same animation in Mecanim

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image anthonyk2 · Feb 17, 2013 at 11:29 PM 0
Share

Same thing here, did you found any solution ? thanks !

6 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by digiben · Feb 17, 2013 at 11:35 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image anthonyk2 · Feb 18, 2013 at 12:02 AM 0
Share

the trick is working, thanks !!!

avatar image Screenhog · Feb 19, 2013 at 09:26 AM 0
Share

Actually, that works great. It's an odd way of thinking about it at first, but in practice, it's quite brilliant. thanks!

avatar image komodor · Feb 25, 2014 at 10:27 AM 0
Share

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

avatar image
11

Answer by komodor · Jan 24, 2014 at 12:14 AM

dunno, but now you can do just:

 Animator.Play(state, layer, normalizedTime);

if you use

 Animator.Play("same state you are", -1, 0f);

it will restart current state, if you put whatever float from 0 to 1 into normalized time, you can set it to any time position you want

you can also use

 Animator.CrossFade(state, crossFadeTime, layer, normalizedTime);
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MegaJiXiang · Feb 23, 2014 at 06:13 AM 0
Share

This doesn't seem to work if I play on enable.

avatar image komodor · Feb 25, 2014 at 10:26 AM 0
Share

I am not sure where the problem is, but it just works for me, i am switching left and right foot with it and there's no problem

avatar image Disastercake · Jan 05, 2015 at 08:10 PM 0
Share

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! =)

avatar image tgsoon2002 · Sep 04, 2015 at 01:25 AM 0
Share

This work perfectly for $$anonymous$$e! Thanks

avatar image simba000 · Oct 07, 2017 at 08:36 AM 0
Share

It does work on the host, but failed to sync across the Network Animator

avatar image
4

Answer by AnomalusUndrdog · Jul 28, 2013 at 03:37 AM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image anthonyk2 · Jul 28, 2013 at 08:29 AM 0
Share

Great news ! Need to try that ;)

avatar image MD_Reptile · Nov 12, 2014 at 07:22 AM 0
Share

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!

avatar image Ash-Blue · Jun 25, 2015 at 07:57 PM 2
Share

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.

avatar image
0

Answer by BenoitFreslon · Dec 06, 2016 at 02:13 PM

Actually the most easiest method is to set a Trigger parameter to relaunch the same animation.

 GetComponent<Animator> ().SetTrigger ( "Play" );

Then

 GetComponent<Animator> ().ResetTrigger ( "Play" );

And setup the Animator like this:

alt text


19909861e61f4746af51d948d27a58f7.png (58.9 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by zachstronaut · Aug 01, 2019 at 06:08 PM

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 uncheck Has Exit Time and check Can Transition to Self on the transition from Any State to your state.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the proper way to wait for an Animator Controller to update? 1 Answer

Mecanim Animations float til end of animation 1 Answer

How can I change a mecanim animation by pressing a key? 1 Answer

Turn off root motion for a specific animation 4 Answers

Timing animations with mecanim 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges